Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7.  
  8. import java.util.concurrent.Semaphore;
  9.  
  10. /**
  11.  *
  12.  * @author ZTI
  13.  */
  14. public class SemaphoresABC {
  15.  
  16.     private static final int COUNT = 10; //Number of letters displayed by threads
  17.     private static final int DELAY = 5; //delay, in milliseconds, used to put a thread to sleep
  18.    
  19.     private static final Semaphore a = new Semaphore(2, true);
  20.     private static final Semaphore b = new Semaphore(0, true);
  21.     private static final Semaphore c = new Semaphore(0, true);
  22.    
  23.     public static void main(String[] args) {
  24.         new A().start(); //runs a thread defined below
  25.         new B().start();
  26.         new C().start();
  27.  
  28.     }
  29.  
  30.     private static final class A extends Thread { //thread definition
  31.  
  32.         @Override
  33.         @SuppressWarnings("SleepWhileInLoop")
  34.         public void run() {
  35.             try {
  36.                 for (int i = 0; i < 2*COUNT; i++) {
  37.                     //use semaphores here
  38.                     a.acquire();
  39.                     System.out.print("A ");
  40.                     //use semaphores here
  41.                     b.release();
  42.                     Thread.sleep(DELAY);
  43.                 }
  44.             } catch (InterruptedException ex) {
  45.                 System.out.println("Ooops...");
  46.                 Thread.currentThread().interrupt();
  47.                 throw new RuntimeException(ex);
  48.             }
  49.             System.out.println("\nThread A: I'm done...");
  50.         }
  51.     }
  52.  
  53.     private static final class B extends Thread {
  54.  
  55.         @Override
  56.         @SuppressWarnings("SleepWhileInLoop")
  57.         public void run() {
  58.             try {
  59.                 for (int i = 0; i < COUNT; i++) {
  60.                     //use semaphores here
  61.                     b.acquire(2);
  62.                     System.out.print("B ");
  63.                     //use semaphores here
  64.                     c.release();
  65.                     Thread.sleep(DELAY);
  66.                 }
  67.             } catch (InterruptedException ex) {
  68.                 System.out.println("Ooops...");
  69.                 Thread.currentThread().interrupt();
  70.                 throw new RuntimeException(ex);
  71.             }
  72.             System.out.println("\nThread B: I'm done...");
  73.         }
  74.     }
  75.  
  76.     private static final class C extends Thread {
  77.  
  78.         @Override
  79.         @SuppressWarnings("SleepWhileInLoop")
  80.         public void run() {
  81.             try {
  82.                 for (int i = 0; i < COUNT; i++) {
  83.                     //use semaphores here
  84.                     c.acquire();
  85.                     System.out.print("C ");
  86.                     //use semaphores here
  87.                     a.release(2);
  88.                     Thread.sleep(DELAY);
  89.                 }
  90.             } catch (InterruptedException ex) {
  91.                 System.out.println("Ooops...");
  92.                 Thread.currentThread().interrupt();
  93.                 throw new RuntimeException(ex);
  94.             }
  95.             System.out.println("\nThread C: I'm done...");
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement