Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SEMAFORO;
- import es.urjc.etsii.code.concurrency.SimpleSemaphore;
- import static es.urjc.etsii.code.
- concurrency.SimpleConcurrent.*;
- public class ej15 {
- static int N_HILOS=4;
- static SimpleSemaphore todos= new SimpleSemaphore(0);
- static SimpleSemaphore exclusion= new SimpleSemaphore(1);
- static volatile boolean ultimo= true;
- static volatile int cont= 0;
- public static void proceso(String letra) {
- while(true) {
- ultimo= true;
- print(letra);
- exclusion.acquire();
- cont++;
- if(cont==4) {
- print("-");
- todos.release(N_HILOS);
- cont=0;
- }
- exclusion.release();
- todos.acquire();
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- createThread("proceso","A");
- createThread("proceso","B");
- createThread("proceso","C");
- createThread("proceso","D");
- startThreadsAndWait();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement