-1

I'm creating client-server based application on Java and I've faced a problem related to threads.

So basically, I've a GUI with list of instances of the same class with different ids for each item. When user clicks to one of them, there pop-ups a window where user can type commands to send to server and receive answer according to id of an item in the list. However, because all of them are instances of the same class, they use the same thread (let's call it "Listen") for each item in the list to receive messages from the server. So the problem is, "Listen" thread jumps from one instance to another during receive operation, which causes change in id and following impossibility to properly receive answer.

The question is, is it possible to bind each thread with item, so it doesn't change during execution of receive method.

1 Answers1

0

Not sure how you handle the instances. But seems like all the execution is happening in the UI thread. You could create a separate thread for each of your instance. If nothing is implemented by your class you could go implementing runnable interface to the class. So that your class instances would be executable threads it self. So you wait in the thread until the response comes. this way you wont need to bind a new thread to your instance. Go through this tutorial quickly : http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html

Juliyanage Silva
  • 2,529
  • 1
  • 21
  • 33