Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.util.concurrent.Semaphore;
- /**
- *
- * @author ZTI
- */
- public class SemaphoresABC {
- private static final int COUNT = 10; //Number of letters displayed by threads
- private static final int DELAY = 5; //delay, in milliseconds, used to put a thread to sleep
- private static final Semaphore a = new Semaphore(2, true);
- private static final Semaphore b = new Semaphore(0, true);
- private static final Semaphore c = new Semaphore(0, true);
- public static void main(String[] args) {
- new A().start(); //runs a thread defined below
- new B().start();
- new C().start();
- }
- private static final class A extends Thread { //thread definition
- @Override
- @SuppressWarnings("SleepWhileInLoop")
- public void run() {
- try {
- for (int i = 0; i < 2*COUNT; i++) {
- //use semaphores here
- a.acquire();
- System.out.print("A ");
- //use semaphores here
- b.release();
- Thread.sleep(DELAY);
- }
- } catch (InterruptedException ex) {
- System.out.println("Ooops...");
- Thread.currentThread().interrupt();
- throw new RuntimeException(ex);
- }
- System.out.println("\nThread A: I'm done...");
- }
- }
- private static final class B extends Thread {
- @Override
- @SuppressWarnings("SleepWhileInLoop")
- public void run() {
- try {
- for (int i = 0; i < COUNT; i++) {
- //use semaphores here
- b.acquire(2);
- System.out.print("B ");
- //use semaphores here
- c.release();
- Thread.sleep(DELAY);
- }
- } catch (InterruptedException ex) {
- System.out.println("Ooops...");
- Thread.currentThread().interrupt();
- throw new RuntimeException(ex);
- }
- System.out.println("\nThread B: I'm done...");
- }
- }
- private static final class C extends Thread {
- @Override
- @SuppressWarnings("SleepWhileInLoop")
- public void run() {
- try {
- for (int i = 0; i < COUNT; i++) {
- //use semaphores here
- c.acquire();
- System.out.print("C ");
- //use semaphores here
- a.release(2);
- Thread.sleep(DELAY);
- }
- } catch (InterruptedException ex) {
- System.out.println("Ooops...");
- Thread.currentThread().interrupt();
- throw new RuntimeException(ex);
- }
- System.out.println("\nThread C: I'm done...");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement