Advertisement
Guest User

ejemplo_deadlock_clase

a guest
Feb 20th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. package ejercicios_sem;
  2.  
  3. import static es.urjc.etsii.code.concurrency.SimpleConcurrent.*;
  4. import es.urjc.etsii.code.concurrency.SimpleSemaphore;
  5.  
  6. //EJEMPLO DEADLOCK EXPLICACION
  7. // SI UNO DE LOS HILOS (PONGAMOS H1) VA MAS RAPIDO Y "COGE" EL PERMISO S1DESPUES DE QUE H2
  8. // COJA S2, NINGUNO DE LOS DOS PODRA CONTINUAR
  9.  
  10. public class Ej_deadlock {
  11.     public static SimpleSemaphore s1;
  12.     public static SimpleSemaphore s2;
  13.    
  14.     public static void h1() {
  15.  
  16.         s1.acquire();
  17.         s2.acquire();
  18.         printlnI("Hilo 1");
  19.         s1.release();
  20.         s2.release();
  21.        
  22.     }
  23.     public static void h2() {
  24.  
  25.         s2.acquire();
  26.         //sleepRandom(100);
  27.         s1.acquire();
  28.         printlnI("Hilo 2");
  29.         s2.release();
  30.         s1.release();
  31.        
  32.     }
  33.    
  34.     public static void main(String[] args) {
  35.         s1 = new SimpleSemaphore(1);
  36.         s2 = new SimpleSemaphore(1);
  37.        
  38.         createThreads(1,"h1");
  39.         createThreads(1,"h2");
  40.        
  41.         startThreadsAndWait();
  42.        
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement