ahjohnston25
ahjohnston25

Reputation: 1975

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 (e.g. 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?

Upvotes: 0

Views: 2276

Answers (2)

Malatesh
Malatesh

Reputation: 1954

"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

Upvotes: 0

robermann
robermann

Reputation: 1722

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

Upvotes: 1

Related Questions