File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Fixed Thread Pools" Watch "Fixed Thread Pools" New topic

Fixed Thread Pools

Kody Wright
Ranch Hand

Joined: Mar 06, 2011
Posts: 32

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

Joined: Oct 16, 2003
Posts: 1101

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.

Jayesh A Lalwani
Saloon Keeper

Joined: Jan 17, 2008
Posts: 2746

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:
subject: Fixed Thread Pools
It's not a secret anymore!