简体   繁体   中英

Share data between java threads

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.

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 (eg a user successfully authenticates to the service).

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.

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.

"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"

I hope you know With BlockingQueue you don't have poll in your code, it internally done JAVA Api

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM