Java Multithreading Semaphore -


if launch example 5 threads in main class running parralell

final int terminal_count = 5;     semaphore semaphore = new semaphore(1);     queue<terminal> terminals = new linkedlist<>();      (int = 1; i<= terminal_count; i++){         terminals.offer(new terminal(i, semaphore));     }      for(terminal terminal : terminals) terminal.start();  } 

and class carries them looks like

public class terminal extends thread { private dispetcher dispetcher; private semaphore semaphore;   public terminal(int terminalid, semaphore semaphore){     dispetcher = new dispetcher();     this.semaphore = semaphore; }  @override public void run() {     try{         listiterator<plane> pit = dispetcher.getplanes().listiterator();         while (pit.hasnext()){             plane p = pit.next();              if(!p.isarrived()) {                 //some code                 replacingpassangers(p);             }         }     }catch (interruptedexception e){         e.printstacktrace();     } }  private void replacingpassangers(plane p) throws interruptedexception{    semaphore.acquire();     if(!p.isarrived()) {         //replacing passangers         p.setisarrived(true);     }     semaphore.release(); } } 

so need after passengers replaced 1 thread others skip methods logic of if(!p.isarriving) condition. p.setisarrived(true); doesn't affects variable in other threads , result threads passing condition... how can fix it?


Comments