并行Observer模式在实时系统中的应用
2018-08-21孙瑜郭新东
孙瑜 郭新东
摘 要: 为了使实时系统中大量数据更新能够及时反馈到相应的模块中,设计了多个主题更新数据的Observer改进模式。该模式使用并行的方式通知相关注册的观察者模块,通过为每个通知方开启独立线程的方式,使得主线程和各模块线程都不被阻塞,并且保证数据更新及时有效。将该设计运用到温室环境监测系统中后,传感数据基本能够及时更新到仪表面板及相应的监测点中,满足一定的实时性。
关键词: Observer; 设计模式; 线程; 实时系统
中图分类号:TP309 文献标志码:A 文章编号:1006-8228(2018)06-64-03
The application of parallel Observer pattern in real time system
Sun Yu, Guo Xindong
(College of Information and Engineering, Shanxi Agricultural University, Jinzhong, Shanxi 030800, China)
Abstract: In order to timely feedback a large number of data updates to the corresponding modules in the real-time system, the improved Observer pattern with multiple theme updating data is designed. This pattern uses a parallel way to notify the related registered observer module by opening an independent thread for each notification party, making the main thread and the module threads not blocked, and ensuring that the data updates are timely and effective. When this design is applied to the greenhouse environmental monitoring system, the sensing data can be updated to the panel and the corresponding monitoring points in time, which meets the need for real time.
Key words: Observer; pattern design; thread; real time system
0 引言
实时性系统通常对数据更新的实时性有一定的要求,如温室环境监测实验平台需要使大量传感设备发送来的原始数据持久化,同时还要对原始数据进行清洗处理,去除异常数据后执行必要运算,然后以可视化的形式呈现在各仪表面板上。传感设备的平均数据发送频率为每秒数次,一个温室中数百个传感器同时回传至服务器上的数据量则达到近千次,若考虑在大型试验田中部署传感网络,回传的数据量将更加庞大,若服务器程序处理不当,则可能发生数据丢失而导致数据不完整[1]。
设计模式中的Observer模式(观察者)定义了主题和观察者两种角色,在同一问题域中,主题和观察者之间实现了一对多的关系。当主题中的状态发生变化时,多个观察者会得到通知,并执行相应的更新动作。Observer模式中的订阅/通知方式可以运用到温室环境监测平台中以解决传感器数据不能及时更新到仪表面板的问题。……
