In a previous post, I used the observer pattern. Description -
class Flight has a status (ie int) - before time, on time, late. This is my Observable class FlightStatusMonitor has an ArrayList of Flights. This class is my observer. There is only one such observer. The update(Observable o, Object arg) method will update the status of the flight and also display the refreshed flight status of all flights that it observes.
I was thinking of using timer tasks to change the status of flights at chosen times and then see the updated status for all flights.
I want to be able to see the flight status displayed on the screen just after it is changed by a timer task.
But, I am not sure if I am doing this correctly. Will concurrency will be a problem here ?
UPDATE I have a set of flights whose status I will change in batches. Batch size can be 1 flight or more - 1 , 5 , 15 ,22 , 45 etc BUT NEVER all flights. I change the status for one batch, a couple of seconds later I change the status for another batch etc. Some flights remain unchanged.
Scenario: Notify Multiple observers on timer event.
Approach :
Sample Code below shows the scenario of combining timer tasks and observers:
// WatchDog.java
For complete details refer to the article: Java timers and observer
As long as the
Observer
doesn't use any mutable state variable you won't have concurrency problems. Even that will be a problem only if you Schedule Task intersect. I mean start one Task before the previous finishes. If tasks are started sequentially it won't be a problem.