If I launch for example five threads in main class that will be running parralell
final int TERMINAL_COUNT = 5;
Semaphore semaphore = new Semaphore(1);
Queue<Terminal> terminals = new LinkedList<>();
for (int i = 1; i<= TERMINAL_COUNT; i++){
terminals.offer(new Terminal(i, semaphore));
}
for(Terminal terminal : terminals) terminal.start();
}
And class that 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 i need that after passengers is replaced by 1 thread the others skip methods logic with help of if(!p.isArriving) condition. But p.setIsArrived(true); doesn't affects this variable in other threads and as result all threads passing this condition... How can I fix it?