Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package multithreading.semaphore.signaling;
- public class MySemaphoreSignaling {
- private boolean wasSignal = false;
- public synchronized void acquire() throws InterruptedException {
- while(!wasSignal) wait();
- System.out.println(Thread.currentThread().getName() + " acquire");
- }
- public synchronized void release() {
- notifyAll();
- wasSignal = true;
- System.out.println(Thread.currentThread().getName() + " release");
- }
- }
- Client:
- package multithreading.semaphore.signaling;
- public class Client {
- public static void main(String[] args) throws InterruptedException {
- MySemaphoreSignaling semaphore = new MySemaphoreSignaling();
- Thread threadA = new Thread(() -> {semaphore.release();});
- Thread threadB = new Thread(() -> {
- try {
- semaphore.acquire();
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- });
- threadA.start();
- threadB.start();
- threadA.join();
- threadB.join();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement