Problem with Java Server Client

Hi I have a very simple client-server program that uses sockets simulating a simple atm. In the client part of the program, I have a gui class and another thread for communication with the server, so that gui and logic are separated, and gui does not freeze, waiting for a thread to communicate with the server.

I create one thread, as the client socket is created on connection and lives through the entire session. And the problem is that I have a loop loop in an infinite loop and a gui request if the user presses any key so that he can take care of the action.

Any suggestions for best practice on this? I do not want to use RMI, as I am developing for educational reasons.

Greetings

+3
source share
2 answers

You can use BlockingQueue . The GUI and client thread will have access to the same queue object. In response to a user action, the graphical interface can queue () command objects. The client thread will still have a while loop, but will use take () to pull command objects out of the queue. take () will block the use of the correct thread synchronization primitives so that you don't have a busy cycle. The server response after the client command is likely to include updating some local state and / or updating the gui, and I would not be surprised if SwingWorker is involved.

There are many ways to solve this, but this is the first that jumped into my head.

+5
source

: , , (TC) , "-" (TBL). TC TBL BlockingQueue (. ).

TC TBL. , , ( SwingUtilities.invokeAndWait() SwingUtilities.invokeLater(), ).

. , ​​ .

+2

All Articles