HOME       >>       Programming

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


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.


Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.