Use Semaphores. Here is a good explanation of Semaphores (read their explanation, skip their code).
- use
Semaphore fooLock
to block execution offoo()
- use
Semaphore barLock
to block execution ofbar()
fooLock
will start unlocked so thatfoo()
is first function to execute.
class FooBar {
private int n;
private Semaphore fooLock = new Semaphore(1);
private Semaphore barLock = new Semaphore(0);
public FooBar(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
fooLock.acquire();
printFoo.run();
barLock.release();
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
barLock.acquire();
printBar.run();
fooLock.release();
}
}
}
- Time Complexity: O(n)
- Space Complexity: O(1)