java - deadlock using Semaphore -


i have simple question managing threads. have 3 process share same semaphore 1 permit. in normal situation, first process takes permit , release 2 permit tho second process. second process release 3 permits third process. given example illustrate problem.

first one:

public class process0 extends thread{  semaphore s;  public process0(semaphore s){     this.s = s; }  public void run(){     try {         sleep(20000);         s.acquire();          system.out.println("hello");     } catch (interruptedexception ex) {         logger.getlogger(process.class.getname()).log(level.severe, null, ex);     }      s.release(2);     } } 

second process:

public class process1 extends thread{  semaphore s;  public process1(semaphore s){     this.s = s; }  public void run(){     try {         this.sleep(10000);         s.acquire(2);         system.out.println("hello 2");     } catch (interruptedexception ex) {         logger.getlogger(process1.class.getname()).log(level.severe, null, ex);     }      s.release(3); } 

}

and last one:

    public class process2 extends thread{      semaphore s;      public process2(semaphore s){         this.s = s;     }     public void run(){         try {              system.out.println("acquire  process 3 ");             s.acquire(3);              system.out.println("hello 3");         } catch (interruptedexception ex) {             logger.getlogger(process2.class.getname()).log(level.severe, null, ex);         }          }      } 

the problem is. when run 3 process , sure process 3 first excute acquire. have deadlock. process 2 never print "hello 3" , process 1 never print "hello 2".why ?

semaphore s = new semaphore(1);

process0 p = new process0(s); process1 p1 = new process1(s); process2 p2 = new process2(s); p.start(); p1.start(); p2.start(); 

your semaphore constructed new semaphore(1), has 1 permit available acquired. call s.acquire(3) never return since semaphore never have 3 permits available. attempt acquire single permit process blocks since acquisitions ordered , process2 arrived "first":

the release method javadoc states acquisition can happen when

some other thread invokes release() method semaphore and current thread next assigned permit.

this minimal, single-thread example show you:

semaphore s = new semaphore(1); s.acquire(2); system.out.println("didn't deadlock!"); 

the solution use semaphore.acquire() requests 1 permit, or semaphore.acquire(1) requests 1 permit.

you need make sure acquire , release same amount of permits, unless have very reason misuse semaphore. javadoc:

there no requirement thread releases permit must have acquired permit calling acquire [or thread releases of permits]. correct usage of semaphore established programming convention in application.

additionally, seems might using wrong synchronizer task. use cyclicbarrier or other class usable synchronization.


Comments

Popular posts from this blog

symfony - TEST environment only: The database schema is not in sync with the current mapping file -

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -