编程要求
请仔细阅读右侧代码,在 Begin-End 区域内进行代码补充,使线程依照先后顺序依次输出JavaThread+线程名。
测试说明
测试输入:无;
预期输出:
JavaThreadAA
JavaThreadBB
JavaThreadCC
JavaThreadAA
JavaThreadBB
JavaThreadCC
JavaThreadAA
JavaThreadBB
JavaThreadCC
JavaThreadAA
JavaThreadBB
JavaThreadCC
JavaThreadAA
JavaThreadBB
JavaThreadCC
下面展示一些
内联代码片
。
package step1;
public class Task {
public static void main(String[] args) throws Exception {
/********* Begin *********/
//在这里创建线程, 开启线程
Object a=new Object();
Object b=new Object();
Object c=new Object();
MyThread t1 = new MyThread("AA",a,b);
MyThread t2 = new MyThread("BB",b,c);
MyThread t3 = new MyThread("CC",c,a);
new Thread(t1).start();
//t1.start();
Thread.sleep(100);
new Thread(t2).start();
//t2.start();
Thread.sleep(100);
new Thread(t3).start();
//t3.start();
Thread.sleep(100);
/********* End *********/
}
}
class MyThread extends Thread {
/********* Begin *********/
Object prev;
Object self;
String threadName;
public MyThread(String threadName,Object prev,Object self) {
this.threadName=threadName;
this.prev=prev;
this.self=self;
}
public void run() {
int count = 5;
while(count > 0){
synchronized(prev) {
synchronized(self) {
System.out.println("Java Thread" + this.threadName);
count--;
self.notify();
}
try {
prev.wait();
}catch(Exception e) {
e.printStackTrace();
}
}
}
System.exit(0);
}
/********* End *********/
}