Java server client thread problem
Hello
I created a thread because I created a client socket when connecting, and it runs through the whole session The problem is that I let the thread loop in an infinite loop and ask the GUI user whether to press any keys so that it can handle these operations
Any suggestions on best practices in this matter? I don't want to use RMI because I developed it for educational reasons
Cheers!
Solution
You can use BlockingQueue GUI and client threads can access the same queue object In response to user actions, the GUI can queue () command objects The client thread still has a while loop, but will use take() to pull the command object from the queue Take () will prevent you from using the correct thread synchronization primitives, so you won't have busy loops The server response after the client command may involve updating some local state and / or updating the GUI. I wouldn't be surprised if swingworker participated
There are many ways to solve this problem, but this is the first way to jump into my mind