第1题:
请完成下列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(_________________);
}
}
第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题:
请完成下列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 ex5_2 implements Runnable
{
static char flag5_2='a';
public static void main(String[] args)
{
ex5_2 obj5_2=new ex5_2();
Thread thread5_2=new Thread(obj5_2);
Thread5_2.start();
thread5_2=new Thread(obj2_2);
thread5_2.start();
threa'd5_2=new Thread(obj2_2);
thread5_2.start();
}
public void run()
{
char myflag5_2;
synchronized(this)
{
______;
}
System.out.println(myflag5_2+"Start");
for(int i=0;i<2;i++)
{
try
{
Thread.sleep(rand(500,2000));
System.out.println(myflag5_2+"Loop");
}catch(InterruptedException ie)
{
System.out.println(ie);
}
}
System.out.println(myflag5_2+"End");
}
final private iht rand(int low,int high)
{
return(______);
}
}
第4题:
下列程序的运行结果是______。 Class C14 implements Runnable { private int i; public C14(int n) { this.i = n; } public void run{) { try { Thread.currentThread().sleep(i); } catch(InterruptedException ie) { System.err.println(ie.tString()); } System.out.println("线程" + Thread.currentThread() .getName + "睡眠了" + i + "毫秒结束"); } } public class Testl4 { public static void main(String[] args) { Thread t = new Thread(new C14(300), "t"); t.start(); } }
A.线程t睡眠了300毫秒结束
B.线程Thread-0睡眠了300毫秒结束
C.线程t睡眠了i毫秒结束
D.线程Thread-0睡眠了i毫秒结束
第5题:
如果使用Thread t=new Test()语句创建一个线程,则下列叙述正确的是
A.Test类一定要实现Runnable接口
B.Test类一定是Thread类的子类
C.Test类一定是Runnable的子类
D.Test类一定是继承Thread类并且实现Runnable接口
第6题:
对于下面语句,不正确的说法是______。 Thread thrObj=new Thread( );
A.系统没有为此线程对象分配资源
B.只能启动或者终止
C.创建了一个空的线程对象
D.可以调用其他方法
第7题:
线程在生命周期中要经历五种状态,在不使用stop()方法的情况下,线程当前处于终止状态,则它的上一个状态是( )。
A.新建状态
B.阻塞状态
C.可运行状态
D.运行状态
第8题:
对于线程的生命周期,下面四种说法正确的有哪些?()
第9题:
Thread.sleep()方法调用后,当等待时间未到,该线程所处状态为()
第10题:
第11题:
第12题:
阻塞状态
运行状态
就绪状态
新建状态
第13题:
对于下面语句的说法,不正确的是( )。Thread thrObj=new Thread( );
A.系统没有为该线程对象分配资源
B.只能启动或者终止
C.创建了-个空的线程对象
D.可以调用其他方法
第14题:
对于下面语句的说法,不正确的是 Thread thrObj=new Thread();
A.系统没有为该线程对象分配资源
B.只能启动或者终止
C.创建了一个空的线程对象
D.可以调用其他方法
第15题:
下列程序的运行结果是______。 class A implements Runnable { int a; iht i = 2; A(int x) { a = x; } public void run() { while(i > 0) { System.out.println("线程" + a); i--; } } } public class Testl3 { public static void main(String[] args) { Thread a1 = new Thread(new A(1)); Thread a2 = new Thread(new A(2)); a1.start(); a2.start(); } }
A.线程1 线程1 线程2 线程2
B.线程1 线程2
C.线程1 线程2 线程1 线程2
D.线程1 线程1 线程1 线程1
第16题:
下列说法中,错误的一项是______。
A.线程一旦创建,则立即自动运行
B.线程创建后需要调用start()方法,将线程置于可运行状态
C.线程处于可运行状态,意味着它可以被调度
D.调用线程的start()方法后,线程也不一定能立即执行
第17题:
如果线程正处于阻塞状态,不能够使线程直接进入可运行状态的情况是( )。
A.sleep方法的时间到
B.获得了对象的锁
C.线程在调用t.join方法后,线程t结束
D.wait方法结束
第18题:
当一个线程处于new状态时,通过什么方法使其进入runnable状态?
第19题:
下列程序通过实现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)
第20题:
线程在生命周期中要经历5种状态,分别是创建状态、就绪状态、运行状态、堵塞状态、()状态。
第21题:
您的应用程序运用了两个线程,名分别为thread One和thread Two thread Two..您需要修改代码防止从线程thread One的执行到thread Two的执行完成。您将如何去做?()
第22题:
第23题:
调用了线程的start()方法,该线程就进入运行状态 (就绪,还要获得CPU使用权)
线程的run()方法运行结束或被未catch的InterruptedException等异常终结,那么该线程进入死亡状态
线程进入死亡状态,但是该线程对象仍然是一个Thread对象,在没有被垃圾回收器回收之前仍可以像引用其它对象一样引用它
线程进入死亡状态后,调用它的start()方法仍然可以重新启动