I have read various posts on semaphore and how they are different from mutex. Between choosing a Binary semaphore and a synchronized block, what factors should I consider to make my decision?
Problem statement: https://leetcode.com/problems/print-foobar-alternately/
Say there are two methods to print "Foo" and "Bar" and you want to print "Foo" and "Bar" alternatively for a given 'n'.
Approach 1:
Solution using Synchronized Block
class FooBar {
private int n;
boolean shouldPrintFoo = true;
public FooBar(int n) {
this.n = n;
}
public synchronized void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
while (!shouldPrintFoo) {
wait();
}
// printFoo.run() outputs "foo"
printFoo.run();
shouldPrintFoo = false;
notifyAll();
}
}
public synchronized void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
while (shouldPrintFoo) {
wait();
}
// printBar.run() outputs "bar"
printBar.run();
shouldPrintFoo = true;
notifyAll();
}
}
}
Approach 2:
Solution using Semaphores
class FooBar {
private int n;
Semaphore foo = new Semaphore(1);
Semaphore bar = new Semaphore(0);
public FooBar(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();
}
}
}
Questions:
- Could there be any scenario where using one versus the other could result in a problem?
- What are the things I should consider or take into account to decide between "synchronized block" and binary semaphores?
- Are there any best practices I should follow? Any references or links that can help to deepen my knowledge on this?