You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.3 KiB
65 lines
1.3 KiB
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 *********/
|
|
}
|