Question

17.7: You shall design a binary Semaphore and the method aquire(), describe/show how this method can...

17.7: You shall design a binary Semaphore and the method aquire(), describe/show how this method can be implemented (permit is a boolean flag which can be set true/ false).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.concurrent.Semaphore;

public class BinarySemaphore {

private final Semaphore countingSemaphore;

public BinarySemaphore(boolean available) {

if (available) {

countingSemaphore = new Semaphore(1, true);

} else {

countingSemaphore = new Semaphore(0, true);

}

}

public void acquire() throws InterruptedException {

countingSemaphore.acquire();

}

public synchronized void release() {

if (countingSemaphore.availablePermits() != 1) {

countingSemaphore.release();

}

}

}

Add a comment
Know the answer?
Add Answer to:
17.7: You shall design a binary Semaphore and the method aquire(), describe/show how this method can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT