0

this is on a class apart from other, the goal is that the passangers go from the airport to A and then to B and then back to the airport, but when the passangers want to go back to the airport from B, most times it appears that they arrived at the airport and then appears other message saying they are going back to the airport, what can i do? Can show other classes aswell

   public void run() {
                    try {
                        Thread.sleep(new Random().nextInt(50)); //time till it gets to the airport
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("The " + currentThread().getName() + " started the trip with " + numPeople + " passangers!");

                    try {
                        Thread.sleep(new Random().nextInt(100));  //time till it gets to A
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ms.addGuiaCondutor(this);

                    synchronized (obj) {
                        try {
                            obj.wait();
                        } catch (InterruptedException e1) {
                            e1.printStackTrace();
                        }
                    }

                    try {
                        Thread.sleep(100); //time till it gets to B
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    fd.addGuiaCondutor(this);

                    try {
                        Thread.sleep(500); //time till it gets to the airport
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("O " + currentThread().getName() + " arrived at the airport!");
                    ga.guiasCondutores.remove(this);

                }

To better understand, this is the output i get, ignoring all other , look at the Driver1

Client 1 is in queue
Client 2 is in queue
Client 3 is in queue
Client 4 is in queue
Client 5 is in queue
Client 6 is in queue
Driver 1 was called to the airport driving: Bus 
Driver 1 is in Queue to Museu Serralves! 
Driver 1 is starting the visit to Museu Serralves! 
Client 7 is in queue 
Client 8 is in queue 
Client 9 is in queue 
Driver 2 was called to the airport driving: Taxi/Uber 
Client 10 is in queue 
Driver 2 is in Queue to Museu Serralves! 
Client 11 is in queue 
Client 12 is in queue 
Driver 3 was called to the airport driving: Bus 
Driver 3 is in Queue to Museu Serralves! 
Driver 1 finished the visit to Museu Serralves! 
Driver 1 is starting the trip to next destiny: Foz do Douro! 
Driver 2 is starting the visit to Museu Serralves! 
Driver 1 is in Queue to Foz do Douro! 
Driver 1 is starting the visit to Foz do Douro! 
Driver 1 arrived at the airport! !Here is the Problem! 
Driver 2 finished visiting Museu Serralves! 
Driver 2 is starting the trip to next destiny: Foz do Douro! 
Driver 1 ended the visit to Foz do Douro! Driver 1 is on is way back to the Airport!

the problem is that driver 1 has to arrive at the airport after the last phrase on the output, not before i can show other classes

Nikos M.
  • 8,033
  • 4
  • 36
  • 43
  • I cant seem to understand the problem, nor the code you posted reflects the question, except if I am missing sth. I dont see any airports assignment. But in any case you seem to have an [ABA problem](https://en.wikipedia.org/wiki/ABA_problem) – Nikos M. May 08 '20 at 11:10
  • an easy way to workaround/solve the ABA problem is to have a counter (initialy zero) which **counts how many times the node/item has moved/changed state**. Thus in being in airport A and moved back to airport A from B would have different value of the counter and thus you can tell which one happened – Nikos M. May 08 '20 at 11:15
  • if i understand correctly after latest update then maybe the whole `run` method should be `synchronized` instead of only a part of it – Nikos M. May 08 '20 at 18:07

0 Answers0