Jump to content
xisto Community
Sign in to follow this  
Vyoma

How To Customise Threadpoolexecutor For Timeout? To have a timeout on Runnable tasks submited

Recommended Posts

I came across a use case where I had to create a pool of threads on a server that listens on a Socket. I was planning on implementing a Thread pool myself, which would:

- Take in requests from the client and queue up
- A pool of threads would pick up each request Socket instance, handle the request and respond back

Doing so would decrease the overhead of Thread creation and their associated garbage collection.

But then, going through documentation, I found the ThreadPoolExecutor (I use Java 1.5). It seemed to be doing all that was required as of the listing above.

Each of the request processing takes an arbitrary time to respond. I want to put a timeout on each request process, so that when it processes, if it does not finish processing for a specified time, the Socket would be responded with a timeout message. The awaitTermination() method seems to put the timeout on the whole Thread pool and not on the induvidual runnable task.

How to I have the tasks timeout?
Is there a way to customize the ThreadPoolExecutor by extending it? Not sure how I can make use of the hook methods like beforeExecute() and afterExecute().

Any help, suggestions or further documentation is much appreciated.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.