Java多线程编程顺序执行
题目
如
class Foo {
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
}
public void second(Runnable printSecond) throws InterruptedException {
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
}
public void third(Runnable printThird) throws InterruptedException {
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}
如何保证first()
、second()
、third()
按顺序执行
思路
使用AtomicInteger
标记当前执行方法位置,执行完成后,修改值,其他线程循环执行查询值。
代码
import java.util.concurrent.atomic.AtomicInteger;
class Foo {
AtomicInteger index = new AtomicInteger(0);
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
index.set(1);
}
public void second(Runnable printSecond) throws InterruptedException {
while (index.get() < 1) {
wait();
}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
index.set(2);
}
public void third(Runnable printThird) throws InterruptedException {
while (index.get() < 2) {
wait();
}
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}
思路2
使用Semaphore
信号量作为方法锁,这种内存消耗稍小
代码
import java.util.concurrent.Semaphore;
class Foo {
Semaphore s2 = new Semaphore(0);
Semaphore s3 = 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();
s2.release();
}
public void second(Runnable printSecond) throws InterruptedException {
s2.acquire();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
s3.release();
}
public void third(Runnable printThird) throws InterruptedException {
s3.acquire();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}