parent
c3bad6627d
commit
7b25fba156
@ -1,64 +0,0 @@
|
||||
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 th1 = new MyThread("AA", a, c);
|
||||
MyThread th2 = new MyThread("BB", c, b);
|
||||
MyThread th3 = new MyThread("CC", b, a);
|
||||
|
||||
th1.start();
|
||||
Thread.sleep(10);
|
||||
th2.start();
|
||||
Thread.sleep(10);
|
||||
th3.start();
|
||||
Thread.sleep(10);
|
||||
System.exit(0);
|
||||
|
||||
/********* End *********/
|
||||
}
|
||||
}
|
||||
|
||||
class MyThread extends Thread {
|
||||
/********* Begin *********/
|
||||
|
||||
String threadName;
|
||||
Object a = null;
|
||||
Object b = null;
|
||||
|
||||
public MyThread(String threadName, Object a, Object b) {
|
||||
super();
|
||||
this.threadName = threadName;
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
public synchronized void run() {
|
||||
|
||||
int count = 5;
|
||||
while (count > 0) {
|
||||
synchronized (a) {
|
||||
synchronized (b) {
|
||||
System.out.println("Java Thread" + this.threadName);
|
||||
count--;
|
||||
b.notify();
|
||||
|
||||
}
|
||||
try {
|
||||
a.wait();
|
||||
} catch (InterruptedException e) {
|
||||
// TODO 自动生成的 catch 块
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/********* End *********/
|
||||
}
|
Loading…
Reference in new issue