在java线程之间共享数据 [英] Share data between java threads

查看:165
本文介绍了在java线程之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在开发一个与GUI管理界面相连的UDP服务器。 UDP服务器作为自己的类运行,并实现 Runnable 接口。

I'm working on a program right now that is a UDP server tied to a GUI administration interface. The UDP server runs as its own class and implements the Runnable interface.

我决定设计程序的最佳方式是将UDP服务器作为后台的线程运行,只有当事件发生时才更新GUI(例如,用户成功地对服务进行身份验证)。

I decided that the best way to design the program would be to have the UDP server run as a thread in the background, and just update the GUI when an event happens (e.g. a user successfully authenticates to the service).

我不太确定当GUI线程不等待时,用于在线程之间传递信息的数据结构是什么本身)对这些信息,因为它正在执行其他任务。我看过一些课程,如 Futures ,但这些只会告诉你什么时候完成任务(这不是一个优先级),而$ code> BlockingQueue 接口,但是使用 Queue 解决方案似乎就像反复轮询Queue,直到信息被推到它上面,然后根据什么信息做出反应被推送到队列。

I'm not quite sure what data structure to use to pass information between the threads when the GUI thread is not waiting (per se) on this information in that it is performing other tasks as well. I've seen some classes like Futures, but these only tell you when the task is done (not really a priority here) and the BlockingQueue interface, but using a Queue solution seems like I'd have to repeatedly poll the Queue until information was pushed onto it, and then react based upon what information was pushed onto the Queue.

有没有一个数据结构可以用来将数据从服务器发送到GUI,以便可以相应更新?

Is there a data structure that I can use to "send" data from the server up to the GUI so it can update accordingly?

推荐答案

如果我明白,可以使用观察者模式。另请参阅可观察类或其他变体模型视图范例。

Probably, if I well understand, you can use the Observer pattern. Check also the Observable class or other variations of the model-view paradigm.

这篇关于在java线程之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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