Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. package EjerciciosConSemaforos;
  2.  
  3. import static es.urjc.etsii.code.concurrency.SimpleConcurrent.*;
  4. import es.urjc.etsii.code.concurrency.SimpleSemaphore;
  5.  
  6. public class Ejercicio14SincronizaciondeBarrera {
  7.  
  8.     private static SimpleSemaphore semaphore = new SimpleSemaphore(5);
  9.     static volatile int contadorBarrera = 0;
  10.     static final int T_BUFFER = 5;
  11.  
  12.     public static void escribir() {
  13.  
  14.         sleep(1000);
  15.         sleepRandom(500);
  16.         print("A");
  17.  
  18.         semaphore.acquire();
  19.         contadorBarrera++;
  20.         while (contadorBarrera < 5);
  21.         semaphore.release();
  22.  
  23.         print("B");
  24.  
  25.     }
  26.  
  27.     public static void main(String[] args) {
  28.  
  29.         createThreads(T_BUFFER, "escribir");
  30.  
  31.         startThreadsAndWait();
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement