线程协作工具类:控制并发流程:https://blog.csdn.net/qq_40794973/article/details/104111726
1 volatile
class Foo {
/**
* second flag = 2
* third flag = 3
*/
private volatile int flag;
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
flag = 2;
printFirst.run();
}
public void second(Runnable printSecond) throws InterruptedException {
while (flag != 2){}
flag = 3;
printSecond.run();
}
public void third(Runnable printThird) throws InterruptedException {
while (flag != 3){}
printThird.run();
}
}
flag不能用static修饰
具体原因不明白,后面加上
2 synchronized
class Foo {
private volatile int flag;
private final Object lock = new Object();
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
synchronized (lock) {
printFirst.run();
flag = 2;
lock.notifyAll();
}
}
public void second(Runnable printSecond) throws InterruptedException {
synchronized (lock) {
while (flag != 2) {
lock.wait();
}
printSecond.run();
flag = 3;
lock.notifyAll();
}
}
public void third(Runnable printThird) throws InterruptedException {
synchronized (lock) {
while (flag != 3) {
lock.wait();
}
}
printThird.run();
}
}
3 Condition
class Foo {
private volatile int flag;
private ReentrantLock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public Foo() {}
public void first(Runnable printFirst) throws InterruptedException {
lock.lock();
try {
printFirst.run();
flag = 2;
condition.signalAll();
} finally {
lock.unlock();
}
}
public void second(Runnable printSecond) throws InterruptedException {
lock.lock();
try {
while (flag != 2) {
condition.await();
}
printSecond.run();
flag = 3;
condition.signalAll();
} finally {
lock.unlock();
}
}
public void third(Runnable printThird) throws InterruptedException {
lock.lock();
try {
while (flag != 3) {
condition.await();
}
printThird.run();
} finally {
lock.unlock();
}
}
}
4 CountDownLatch
class Foo {
private CountDownLatch secondLatch = new CountDownLatch(1);
private CountDownLatch thirdLatch = new CountDownLatch(1);
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
printFirst.run();
secondLatch.countDown();
}
public void second(Runnable printSecond) throws InterruptedException {
secondLatch.await();
printSecond.run();
thirdLatch.countDown();
}
public void third(Runnable printThird) throws InterruptedException {
thirdLatch.await();
printThird.run();
}
}
5 Semaphore
class Foo {
private Semaphore secondSemaphore = new Semaphore(0);
private Semaphore thirdSemaphore = new Semaphore(0);
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
printFirst.run();
secondSemaphore.release();
}
public void second(Runnable printSecond) throws InterruptedException {
secondSemaphore.acquire();
printSecond.run();
thirdSemaphore.release();
}
public void third(Runnable printThird) throws InterruptedException {
thirdSemaphore.acquire();
printThird.run();
}
}
6 CyclicBarrier
class Foo {
private CyclicBarrier secondBarrier = new CyclicBarrier (2);
private CyclicBarrier thirdBarrier = new CyclicBarrier (2);
public Foo() {}
public void first(Runnable printFirst) throws InterruptedException {
try {
printFirst.run();
secondBarrier.await();//虽然在等待,但是我已经输出结果了
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
public void second(Runnable printSecond) throws InterruptedException {
try {
secondBarrier.await();
printSecond.run();
thirdBarrier.await();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
public void third(Runnable printThird) throws InterruptedException {
try {
thirdBarrier.await();
printThird.run();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
}