參考:
https://blog.csdn.net/qq_32655383/article/details/51660925
但是我有點看不明白,于是就自己改寫了下,後面分析。
線程類:
package threadabc;
public class ThreadPrint implements Runnable { //A
private Object after; // Object b 下一個要執行的對象鎖
private Object now; //Object a 目前對象鎖
private String name;
public ThreadPrint(String name, Object after, Object now) {
this.name = name;
this.now = now;
this.after = after;
}
@Override
public void run() {
for (int i=0;i<100;i++){
System.out.println(name); //A
synchronized (now) {
synchronized (after) {
after.notify(); //B
}
try {
now.wait(); //線程A放入a鎖
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Main主函數:
public static void main(String[] args) {
Object a = new Object();
Object b = new Object();
Object c = new Object();
ThreadPrint threadPrintA = new ThreadPrint("A",b,a);
ThreadPrint threadPrintB = new ThreadPrint("B",c,b);
ThreadPrint threadPrintC = new ThreadPrint("C",a,c);
Thread threadA = new Thread(threadPrintA);
Thread threadB = new Thread(threadPrintB);
Thread threadC = new Thread(threadPrintC);
threadA.start();
threadA.sleep(100);
threadB.start();
threadB.sleep(100);
threadC.start();
}
關鍵:
a對象鎖對應A線程
b對象鎖對應B線程
c對象鎖對應C線程
流程:
1. 執行A線程:
a對象,b對象傳入A線程
當A線程執行完成後,将A線程關入a對象鎖中,并且從b對象鎖中喚醒B線程,因為b鎖中還沒有B線程是以沒用。
-
因為按照主線程順序,接下來執行線程B:
b對象,c對象傳入A線程
當B線程執行完成後,将B線程關入b對象鎖中,并且從c對象鎖中喚醒C線程,因為c對象鎖中還沒有C線程是以沒用。
-
因為按照主線程順序,接下來執行線程C:
c對象,a對象傳入A線程
當C線程執行完成後,将C線程關入c對象鎖中,并且從a對象鎖中喚醒A線程。此時隻有A線程執行任務。
由此一個循環結束,開啟重複循環