Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. package SEMAFORO;
  2.  
  3. import es.urjc.etsii.code.concurrency.SimpleSemaphore;
  4. import static es.urjc.etsii.code.
  5. concurrency.SimpleConcurrent.*;
  6. public class ej15 {
  7.     static int N_HILOS=4;
  8.     static SimpleSemaphore todos= new SimpleSemaphore(0);
  9.     static SimpleSemaphore exclusion= new SimpleSemaphore(1);
  10. static volatile boolean ultimo= true;
  11. static volatile int cont= 0;
  12.    
  13.     public static void proceso(String letra) {
  14.         while(true) {
  15.             ultimo= true;
  16.         print(letra);
  17.         exclusion.acquire();
  18.         cont++;
  19.         if(cont==4) {
  20.             print("-");
  21.             todos.release(N_HILOS);
  22.             cont=0;
  23.         }      
  24.        
  25.         exclusion.release();       
  26.         todos.acquire();
  27.        
  28.         }
  29.    
  30. }
  31.     public static void main(String[] args) {
  32.         // TODO Auto-generated method stub
  33.         createThread("proceso","A");
  34.         createThread("proceso","B");
  35.         createThread("proceso","C");
  36.         createThread("proceso","D");
  37.        
  38.         startThreadsAndWait();
  39.        
  40.        
  41.  
  42.     }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement