0

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:

  1. Could there be any scenario where using one versus the other could result in a problem?
  2. What are the things I should consider or take into account to decide between "synchronized block" and binary semaphores?
  3. Are there any best practices I should follow? Any references or links that can help to deepen my knowledge on this?
SyncMaster
  • 9,206
  • 31
  • 89
  • 129
  • See: [binary-semaphore-vs-a-reentrantlock](https://stackoverflow.com/questions/17683575/binary-semaphore-vs-a-reentrantlock), [java-binary-semaphore-vs-reentrant-lock](https://www.baeldung.com/java-binary-semaphore-vs-reentrant-lock) – zysaaa Jan 04 '22 at 08:21

0 Answers0