Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ejercicios_sem;
- import static es.urjc.etsii.code.concurrency.SimpleConcurrent.*;
- import es.urjc.etsii.code.concurrency.SimpleSemaphore;
- //EJEMPLO DEADLOCK EXPLICACION
- // SI UNO DE LOS HILOS (PONGAMOS H1) VA MAS RAPIDO Y "COGE" EL PERMISO S1DESPUES DE QUE H2
- // COJA S2, NINGUNO DE LOS DOS PODRA CONTINUAR
- public class Ej_deadlock {
- public static SimpleSemaphore s1;
- public static SimpleSemaphore s2;
- public static void h1() {
- s1.acquire();
- s2.acquire();
- printlnI("Hilo 1");
- s1.release();
- s2.release();
- }
- public static void h2() {
- s2.acquire();
- //sleepRandom(100);
- s1.acquire();
- printlnI("Hilo 2");
- s2.release();
- s1.release();
- }
- public static void main(String[] args) {
- s1 = new SimpleSemaphore(1);
- s2 = new SimpleSemaphore(1);
- createThreads(1,"h1");
- createThreads(1,"h2");
- startThreadsAndWait();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement