本文共 1728 字,大约阅读时间需要 5 分钟。
但是我有点看不明白,于是就自己改写了下,后面分析。
线程类: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线程关入b对象锁中,并且从c对象锁中唤醒C线程,因为c对象锁中还没有C线程所以没用。
当C线程执行完成后,将C线程关入c对象锁中,并且从a对象锁中唤醒A线程。此时只有A线程执行任务。
由此一个循环结束,开启重复循环
转载地址:http://birxx.baihongyu.com/