多线程按顺序打印
来源于leetcode1114
我们提供了一个类:
public class Foo {
public void first() { print(“first”); }
public void second() { print(“second”); }
public void third() { print(“third”); }
}
三个不同的线程将会共用一个 Foo 实例。
- 线程 A 将会调用 first() 方法
- 线程 B 将会调用 second() 方法
- 线程 C 将会调用 third() 方法
请设计修改程序,以确保 second() 方法在 first() 方法之后被执行,third() 方法在 second() 方法之后被执行。
本文提供四种方法ReentrantLock,wait-notify,semaphore,cas自旋。其他方法还可以用CountdownLatch,ArrayBlockingQueue。
方法一
//第一种方法用ReentrantLock
class Foo {
private volatile int flag = 1;
private ReentrantLock reentrantLock = new ReentrantLock();
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
for (;;) {
if (flag == 1) {
reentrantLock.lock();
break;
}
}
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
flag = 2;
reentrantLock.unlock();
}
public void second(Runnable printSecond) throws InterruptedException {
for (;;) {
if (flag == 2) {
reentrantLock.lock();
break;
}
}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
flag = 3;
reentrantLock.unlock();
}
public void third(Runnable printThird) throws InterruptedException {
for (;;) {
if (flag == 3) {
reentrantLock.lock();
break;
}
}
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
reentrantLock.unlock();
}
}
方法二
//既然都volatile的flag了,直接下面这么写
class Foo {
private volatile int flag = 1;
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
while (flag != 1) {
}
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
flag = 2;
}
public void second(Runnable printSecond) throws InterruptedException {
while (flag != 2) {
}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
flag = 3;
}
public void third(Runnable printThird) throws InterruptedException {
while (flag != 3) {
}
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}
方法三
//wait-notify
class Foo {
private Object object = new Object();
private volatile int flag = 1;
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
synchronized (object) {
while (flag != 1) {
object.wait();
}
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
flag = 2;
object.notifyAll();
}
}
public void second(Runnable printSecond) throws InterruptedException {
synchronized (object) {
while (flag != 2) {
object.wait();
}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
flag = 3;
object.notifyAll();
}
}
public void third(Runnable printThird) throws InterruptedException {
synchronized (object) {
while (flag != 3) {
object.wait();
}
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
object.notifyAll();
}
}
}
方法四
//信号量
class Foo {
private Semaphore semaphore1 = new Semaphore(0);
private Semaphore semaphore2 = new Semaphore(0);
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
semaphore1.release();
}
public void second(Runnable printSecond) throws InterruptedException {
semaphore1.acquire();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
semaphore2.release();
}
public void third(Runnable printThird) throws InterruptedException {
semaphore2.acquire();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
semaphore2.release();
}
}