Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two threads - one does the work

 
Jason Hocker
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I want. I have an application that has a ServerSocket for incoming transactions. Usually this is one thread per incoming socket connection.
What I want is to add a second thread to do all the work. So the data comes in from the socket, and a second thread starts. When the second thread needs more data, it will wait for the first thread to send a response over the socket, get another request, and then have the second thread continue with this new data.
Im having trouble implementing this second thread. I'm confused how to have thread 1 wait while thread 2 works, and then have thread 2 wait while thread 1 gets the data.
I'm guessing that I'll need to store all instances of "thread 2" in a hashtable for the "thread 1"s to use in later transactions.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give more explanation?
U can have one thread that has method sendRequest(request) and the thread will write all replies to a buffer. So now, the second thread reads the buffer and if it needs to send a request it will use thread1.sendRequest()

u can have the thread1 to be a singletone.
 
Jason Hocker
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where I am getting confused is when to say notifyAll() and wait(), and what object to synchronize.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay, let me ask you. Dou you have to serve only one conenction at a time and don't want to start a new thread for each one? Or rather, you have to serve several connections simultaneously and you want only one thread for that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic