This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Fixed Thread Pools Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Fixed Thread Pools" Watch "Fixed Thread Pools" New topic
Author

Fixed Thread Pools

Kody Wright
Ranch Hand

Joined: Mar 06, 2011
Posts: 32
Hello,

I started a thread on this forum last week about dealing with client/server connections and starting a new thread
each time a client connected. Jayesh A Lalwani helped me in solving the issue and then gave me some suggestions
about how I could improve the design, which I greatly appreciated.

After doing some research on thread pools I tried to implement them into my program and succeeded...partially. I
have a few questions regarding the appropriate use of thread pools and was hoping someone more experienced here
could help me understand them a little better.

Firstly, my RejectedExecutionException handler is not catching the error whenever I try to connect an additional
client...instead the second client will simply hang.

Secondly, if I connect two clients, the first one will connect and function correctly. The second one will accept
and send text but will not receive anything since it is waiting in the que...however if I send a message with the
second client while it is waiting then proceed to send a message with the first client suddenly both clients will
receive the messages and start functioning correctly. How can this happen?

Thirdly, how can I stop an individual thread that exists within an executor while leaving the other threads alone?

I appreciate your time and help!



Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

For your second problem, I think you need to call flush() in your output streams after writing.

For your third problem, threads should be stopped using interrupts. You should use submit() instead of execute() which returns a Future instance. And then you can call cancel() on that instance which will interrupt the thread.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2415
    
  28

Can you put some more logs in your Runnable and post what you see?. Also, are both of your clients getting the "succesfuly connected..." message?

Why do you want to stop individual threads? You shouldn't be stoppign threads. You should let the executor manage it's own threads.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Fixed Thread Pools