Java - Combine Observer pattern with timer task?

不羁岁月 提交于 2020-01-04 08:06:07

问题


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.

The related post


回答1:


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.




回答2:


Scenario: Notify Multiple observers on timer event.

Approach :

  • Create Watchdog class which creates the timer and timer task.
  • Observers register to Watchdog event.
  • Watchdog class on timer event notifies the observers.

Sample Code below shows the scenario of combining timer tasks and observers:

// WatchDog.java

import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;

// Observer class

class Observer1 implements Observer{

@Override
public void update(Observable arg0, Object arg1) {
System.out.println("Observer1 notified");
}
}

// Watchdog Component which creates the timer and notifies the timers.

public class WatchDog extends Observable {
    Timer timer;
    int seconds;

   // Notify task to notify observers

    class NotifyTask extends TimerTask{

        @Override
        public void run() {
        setChanged();
        notifyObservers();
        }
    }

    public WatchDog( ) {
        timer = new Timer(); 
    }

    public void schedule(long seconds){
        timer.scheduleAtFixedRate(new NotifyTask(), 0, seconds*1000); //delay in milliseconds

    }

    public void stop(){
    timer.cancel();
    }

    public static void main(String args[]) throws InterruptedException {
        Observer1 observer1 = new Observer1();

        WatchDog watchDog = new WatchDog();
        // register with observer
        watchDog.addObserver(observer1);

        System.out.println("WatchDog is scheduled.");
        watchDog.schedule(5);
        Thread.sleep(25000);

        watchDog.stop();
    }
}

For complete details refer to the article: Java timers and observer



来源:https://stackoverflow.com/questions/15657398/java-combine-observer-pattern-with-timer-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!