主方法
class FooBarTest {
public static void main(String[] args) {
FooBar fooBar = new FooBar(3);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
fooBar.foo(new Runnable() {
@Override
public void run() {
System.out.print("foo");
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
fooBar.bar(new Runnable() {
@Override
public void run() {
System.out.print("bar\n");
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
}
}
可重入锁 + Condition实现
class FooBar4 {
private int n;
public FooBar4(int n) {
this.n = n;
}
Lock lock = new ReentrantLock(true);
private final Condition foo = lock.newCondition();
volatile boolean flag = true;
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
lock.lock();
try {
while (!flag) {
foo.await();
}
printFoo.run();
flag = false;
foo.signal();
} finally {
lock.unlock();
}
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
lock.lock();
try {
while (flag) {
foo.await();
}
printBar.run();
flag = true;
foo.signal();
} finally {
lock.unlock();
}
}
}
}
synchronized同步锁+唤醒实现
class FooBar5 {
private int n;
private volatile boolean type = true;
private final Object foo= new Object();
public FooBar5(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
synchronized (foo) {
while(!type){
foo.wait();
}
printFoo.run();
type = false;
foo.notifyAll();
}
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
synchronized (foo) {
while(type){
foo.wait();
}
printBar.run();
type = true;
foo.notifyAll();
}
}
}
}
class FooBar6 {
private int n;
private Semaphore foo = new Semaphore(1);
private Semaphore bar = new Semaphore(0);
public FooBar6(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
foo.acquire();
printFoo.run();
bar.release();
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
bar.acquire();
printBar.run();
foo.release();
}
}
}