Runnable r = new Runnable() { public void run() { System.out.print(”Cat”); } }; Threadt=new Thread(r) { public void run() { System.out.print(”Dog”); } }; t.start(); What is the result?()
第1题:
请阅读下面程序 public class ThreadTest{ public static void main(String args[]) ( Thread t1=new Thread(new Hello()); Thread t2=new Thread(new Hello()); t1.start(); t2.start(); } } class Hello implements Runnable { int i; public void run() { while(true) { System.out.prinfin("Hello"+i++); if(i=5) break; } } } 该程序创建线程使用的方法是
A.继承Thread类
B.实现Runnable接口
C.t1.start()
D.t2.start()
第2题:
通过实现Runnable接口创建线程,请在下面横线处填入代码完成此程序。
注意:不改动程序结构,不得增行或删行。
class ThreadTest implements Runnable
{
Thread thrObj;
public static void main(String args[])
{
System.out.println("这是一个通过实现接口创建线程的例子");
ThreadTest testObj=new ThreadTest();
testObj.create();
}
public void create()
{
if(thrObj= =null)
{
thrObj=new Thread(this,"myThread");
______
}
}
public void run()
{
System.out.println("Thread"+throbj.getName()+":"+"在运行!");
}
}
第3题:
当一个线程处于new状态时,通过什么方法使其进入runnable状态?
第4题:
下列程序通过实现Runnable接口创建一个线程,选择正确的语句填入程序的横线处。 class MyRun implements Runnable { String str; MyRun(String s) { str = s; } public void run() System.out.println(str); } } public class ex40 { public static void main(String[] args) { String name = "实现阶段Runnable 接口"; MyRun my = new MyRun(name); Thread th = th. start ( ); } }
A.new MyRun(my)
B.new Thread()
C.new Thread(my)
D.Thread(my)
第5题:
public class Threads5 { public static void main (String[] args) { new Thread(new Runnable() { public void run() { System.out.print(”bar”); } }).start(); } } What is the result?()
第6题:
public class Threads 1 { intx=0; public class Runner implements Runnable { public void run() { int current = 0; for(int=i=0;i<4;i++){ current = x; System.out.print(current + “, “); x = current + 2; } } } public static void main(String[] args) { new Threads1().go(); } public void go() { Runnable r1 = new Runner(); new Thread(r1).start(); new Thread(r1 ).start(); } } Which two are possible results?()
第7题:
public class Threads4 { public static void main (String[] args) { new Threads4().go(); } public void go() { Runnable r = new Runnable() { public void run() { System.out.print(”foo”); } }; Thread t = new Thread(r); t.start(); t.start(); } } What is the result?()
第8题:
Code block a.
Code block B.
Code block c.
Code block d.
Code block e.
第9题:
0, 2, 4, 4, 6, 8, 10, 6,
0, 2, 4, 6, 8, 10, 2, 4,
0, 2, 4, 6, 8, 10, 12, 14,
0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
第10题:
SomeThread类是包含run()方法的任意java类
SomeThread类一定要实现Runnable接口
SomeThread类是Thread类的子类
SomeThread类是Thread类的子类并且要实现Runnable接口
第11题:
Extend java.lang.Thread and override the run method.
Extend java.lang.Runnable and override the start method.
Implement java.lang.thread and implement the run method.
Implement java.lang.Runnable and implement the run method.
Implement java.lang.Thread and implement the start method.
第12题:
AAA
BBB
Compilation fails.
The code runs with no output.
第13题:
请完成下列Java程序:程序的功能演示了如何通过实现Runnable接口创建线程对象,程序中定义了一个类B,类中重写了含一个字符串参数的构造方法,并实现了Runnable接口,即在类B中编写了接口中的run()方法的方法体。还定义了一个应用程序类ex35_2,其中创建类B的3个对象b1,b2和b3作为线程对象t1,t2和t3的参数,并启动这3个线程。
注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
程序运行结果如下:
public class ex35_2
{
public static void main(String args[ ])
{
Runnable b1=new B("First");
Runnable b2=new B("Second");
Runnable b3=new B("Third");
Thread t1=new Thread(b1);
Thread t2=new Thread(b2);
Thread t3=new Thread(b3);
t1.start ();
t2.start ();
t3.start();
}
}
class B _____________________ Runnable
{
String s;
public B(String str)
{
s=str;
}
_________________
{
for(int i=1;i<3;i++)
{
System. out. println ( s+ "运行!");
try
{
Thread.sleep((int) (Math.random() *100) );
}
catch (InterruptedException e)
{
e.printStackTrace ( );
}
}
System. out.println (s+"结束!");
}
}
第14题:
如果使用Thread t=new Test()语句创建一个线程,则下列叙述正确的是
A.Test类一定要实现Runnable接口
B.Test类一定是Thread类的子类
C.Test类一定是Runnable的子类
D.Test类一定是继承Thread类并且实现Runnable接口
第15题:
阅读下面程序 public class Test implements Runnable { public static void main(String[] args) { ______ t.start(); } public void run() { System.out.println("Hello!"); } } 程序中下画线处应填入的正确选项是
A.Test t=new Test();
B.Thread t=new Thread();
C.Thread t=new Thread(new Test());
D.Test t=new Thread();
第16题:
class MyThread extends Thread { public void run() { System.out.println(“AAA”); } public void run(Runnable r) { System.out.println(“BBB”); } public static void main(String[] args) { new Thread(new MyThread()).start(); } } What is the result?()
第17题:
Which two code fragments will execute the method doStuff() in a separate thread?()
第18题:
现有: class ThreadBoth extends Threaa implements Runnable { public void run() (System.out.print("hi"); } public static voicl main (String [] args) { Thread tl=new ThreadBoth(): Thread t2 = new Thread (tl): tl.run(): t2.run(): } 结果为:()
第19题:
Which two code fragments will execute the method doStuff() in a separate thread?()
第20题:
Compilation fails.
An exception is thrown at runtime.
The code executes normally and prints „foo”.
The code executes normally, but nothing is printed.
第21题:
Cat
Dog
Compilation fails.
The code runs with no output.
An exception is thrown at runtime.
第22题:
new Thread() { public void run() { doStuff(); } }
new Thread() { public void start() { doStuff(); } }
new Thread() { public void start() { doStuff(); } } .run();
new Thread() { public void run() { doStuff(); } } .start();
new Thread(new Runnable() { public void run() { doStuff(); } } ).run();
new Thread(new Runnable() { public void run() { doStuff(); } }).start();
第23题:
Compilation fails.
An exception is thrown at runtime.
The code executes normally and prints “bar”.
The code executes normally, but nothing prints.