ReentrantLock实现abc循环输出

import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class ReetlockABC { public static void main(String[] args) { // TODO Auto-generated method stub final Out out = new Out(); new Thread(new Runnable() {@Override public void run() { // TODO Auto-generated method stub try { out.pB(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); new Thread(new Runnable() {@Override public void run() { // TODO Auto-generated method stub try { out.pA(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); new Thread(new Runnable() {@Override public void run() { // TODO Auto-generated method stub try { out.pC(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); }}class Out { Lock lock = new ReentrantLock(); Condition a_b = lock.newCondition(); //可以理解为等待a唤醒b Condition b_c = lock.newCondition(); Condition c_a = lock.newCondition(); int flag = 1; public void pA() throws InterruptedException { while (true) { lock.lock(); while (flag != 1) c_a.await(); System.out.println("a"); flag = 2; a_b.signal(); lock.unlock(); } } public void pB() throws InterruptedException { while (true) { lock.lock(); while (flag != 2) a_b.await(); System.out.println("b"); flag = 3; b_c.signal(); lock.unlock(); } } public void pC() throws InterruptedException { while (true) { lock.lock(); while (flag != 3) b_c.await(); System.out.println("c"); flag = 1; c_a.signal(); lock.unlock(); } } }

以前我用synchronized同步块时,当有三个或以上的线程时必须用obj.signalAll(); (obj.signal()可能会唤醒错人导致死锁或者用嵌套的synchronized(麻烦死了而且容易死锁)),用lock的Condition可以唤醒指定对象,不用唤醒全部

    推荐阅读