Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
771 views
in Technique[技术] by (71.8m points)

multithreading - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...