第1题:
如果使用 Thread t = new Test() 语句创建一个线程,则下列叙述正确的是
A)Test 类一定要实现 Runnable 接口
B)Test 类一定是 Thread 类的子类
C)Test 类一定是 Runnable 的子类
D)Test 类一定是继承 Thread 类并且实现 Runnable 接口
第2题:
请阅读下面程序 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()
第3题:
( 24 )请阅读下面程序
public class ThreadTest {
public static void main ( String args[ ]){
Thread t1 = new Thread ( new Hello ()):
Thread t2 = new Thread ( new Hello ()):
t l .start ():
t2.start ();
}
}
class Hello implements Runnable {
int i ;
public void run (){
while ( true ) {
System.out.println ( "Hello"+i++ ) ;
if ( i=5 ) break :
}
}
}
该程序创建线程使用的方法是()
A )继承 Thread 类
B )实现 Runnable 接口
C ) t l.start ()
D ) t2.start ()
第4题:
对于下面语句的说法,不正确的是( )。Thread thrObj=new Thread( );
A.系统没有为该线程对象分配资源
B.只能启动或者终止
C.创建了-个空的线程对象
D.可以调用其他方法
第5题:
请完成下列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+"结束!");
}
}
第6题:
语句Thread thread1=new SomeThreadClass()成功运行后,线程thread1处于生命周期的______状态。
第7题:
请完成下列Java程序:运行3个线程,每一个线程有自己的标志,用a,b,c表示,每个线程显示一个“Start”信息和一个“End”信息并且间隔地显示2个“Loop”信息(间隔变化为(0.5-2)秒之间的随机延迟)。
程序运行结果如下:(注:由于时间间隔为随机数,所以,运行结果的顺序不惟一)
a Start
b Start
c Start
b Loop
a Loop
b Loop
b End
c Loop
a Loop
a End
c Loop
c End
注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
public class ex2_2 implements Runnable {
static char flag2_2 = 'a';
public static void main(String[] arg) {
ex2_2 obj2_2 = new ex2_2();
Thread thread2_2 = new Thread(obi2_2);
thread2_2.start();
thread2_2 = new Thread(obj2_2);
thread2 2.start();
thread2_2 = new Thread(obi2_2);
thread2_2, start ( );
}
public void run() {
char myflag2_2;
synchronized(this) {
_________________;
}
System.out.println(myflag2_2 +" Start");
for(int i=0; i<2; i++) {
try {
Thread.sleep(rand(500,2000));
System.out.println(myflag2_2 +" Loop");
} catch(InterruptedException ie) {
System.out.println(ie);
}
}
System.out.println(myflag2_2 +" End");
}
final private int rand(int low, int high)
return(_________________);
}
}
第8题:
通过实现Rmmable接口创建线程,请在下面横线处填写代码完成此程序。
public class ThreadTest
{
public static void main(String args [])
{
Thread testObj1 = new Thread (new Hello ());
Thread testObj2 = new Thread (new Hello ());
testObj 2.start ( );
}
}
class Hello implements Runnable
{
int j;
public void run()
{
System.out.println("Hello" + j ++);
}
}
第9题:
Thread类中能运行线程的方法是( )。
A.resume( )
B.start( )
C.run( )
D.init( )
第10题:
通过实现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()+":"+"在运行!");
}
}