Java-将Observer模式与计时器任务结合在一起? [英] Java - Combine Observer pattern with timer task?

查看:72
本文介绍了Java-将Observer模式与计时器任务结合在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一篇文章中,我使用了观察者模式.说明-

In a previous post, I used the observer pattern. Description -

类Flight的状态(即int)-在时间,准时,延迟之前.这是我的观察 FlightStatusMonitor类具有一个Flight的ArrayList.这个班是我的观察员.这样的观察者只有一个. update(Observable o,Object arg)方法将更新航班状态,并显示其观察到的所有航班的刷新航班状态.

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 ?

更新 我有一组航班,它们的状态将成批更改.批次大小可以是1个航班或更多-1,5,15,22,45等,但绝不要所有航班.我更改了一个批次的状态,几秒钟后,我更改了另一个批次的状态,等等.某些航班保持不变.

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.

相关帖子

推荐答案

只要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.

这篇关于Java-将Observer模式与计时器任务结合在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆