This discussion is archived
10 Replies Latest reply: Jun 6, 2011 3:33 AM by 802316 RSS

ThreadPoolExecutor- Thread termination

866397 Newbie
Currently Being Moderated
I created a thread pool executor and submitted some callable tasks to thread pool and waiting on future object in the method futureobject.get(timeout, TimeUnit.SECONDS).
Once it is timed out i am catching the TimeoutException and cancelling the task through future.cancel(ture),
But it is just interrupting the thread and not cancelling the task if the task has already started executing.

Is there any way to stop the future Object task after a certain time out irrespective of its current state?
I want to terminate the thread execution and make the thread pick another task once if it is timed out.
One way is to interrupt the thread which is handling the future object task and then stop the thread.

Is there any other way that I can cancel a future Object task directly without dealing with the thread handling this future object task.
Can you throw a light on this. Any idea would be helpful...!!

Thanks in advance.
  • 1. Re: ThreadPoolExecutor- Thread termination
    802316 Pro
    Currently Being Moderated
    863394 wrote:
    Is there any way to stop the future Object task after a certain time out irrespective of its current state?
    Get a reference to the Thread and call stop(). BTW it generally a bad idea, but it can be done.
    I want to terminate the thread execution and make the thread pick another task once if it is timed out.
    You should change your task so it is interruptible.
    One way is to interrupt the thread which is handling the future object task and then stop the thread.
    Future.cancel(true) interrupts the thread if it has started. Stopping the Thread is likely to be harder to get right than making your task interruptible. (possibly impossible)
    Is there any other way that I can cancel a future Object task directly without dealing with the thread handling this future object task.
    No. cancel() is the only way to cancel.
  • 2. Re: ThreadPoolExecutor- Thread termination
    866397 Newbie
    Currently Being Moderated
    Thanks Peter for your reply.

    But the problem is the thread is not like executing in a while loop to test every time whether it is interrupted or not by future.cancel(ture).
    As my callable task is calling internally more methods which are call some more methods of other classes in this i cant put every where this logic to check whether thread is interrupted or not.
    Even i have done that my concern is what if thread is executing a method long time because of some blocking problem. i need to wait the control comes back after method to check whether thread is interrupted or not. which is not desired..

    let me put in an example.

    public Object call(){
    new ArrayList list=SampleClass1Object.readData();

    // check whether thread is interrupt by future.cancel() method

    Object ob= SampleClass2Object.display(list);
    return ob;
    }
    If i fix time in future.get(timeout,TimeUnit). when the time out is happend the thread executing the method readData() it wont reflect immediately once this readData() method is completed then only it will check whether the thread is interrupted or not. This what is happening right now in my case.
  • 3. Re: ThreadPoolExecutor- Thread termination
    802316 Pro
    Currently Being Moderated
    What I would do, as I have done in the past is to get a StackTrace of the thread which is running too long.

    Print out the stack trace at regular intervals. e.g. if the time is 10 seconds, after 10, 12, 14, 16, etc seconds.

    Either fix the code so it doesn't take so long or get it to support interrupts.

    Or use Thread.stop() and accept that this can leave your application in an undesirable state.
  • 4. Re: ThreadPoolExecutor- Thread termination
    866397 Newbie
    Currently Being Moderated
    Thank you Very much Peter for you solution.

    But why the thread is not throwing an interrupted exception even if future.cancel() method is interrupting the thread. All I want to do is resubmiting the task again in thread pool once if it get timed out. it will be possible by terminating the task (killing the thread or thread should release the task) once time out happens.
  • 5. Re: ThreadPoolExecutor- Thread termination
    EJP Guru
    Currently Being Moderated
    InterruptedException is only thrown when executing a method that throws it, e.g. wait(), sleep(), join(), waitFor(), etc. Does your thread do that?
  • 6. Re: ThreadPoolExecutor- Thread termination
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Don't double post the same question. Your other thread posted in 'Java Programming' has been removed.

    That thread had one response, reproduced below.

    db
    YoGee wrote:
    The callable object needs to check whether it has been interrupted (periodically call Thread.interrupted() to see if it should exit)
  • 7. Re: ThreadPoolExecutor- Thread termination
    866397 Newbie
    Currently Being Moderated
    what if thread is hung in connecting to a database and waiting for a query to execute. In this case I need to terminate the thread but the only way to do that is to check whether thread is interrupted or not as you suggested, for this I need to wait until my control comes back? Even it comes back that means the thread is running not in hung state. Is there any way that i could kill the thread as soon as timed out happens??
  • 8. Re: ThreadPoolExecutor- Thread termination
    EJP Guru
    Currently Being Moderated
    The JDBC driver should have a configurable timeout parameter.
  • 9. Re: ThreadPoolExecutor- Thread termination
    798906 Newbie
    Currently Being Moderated
    Forcing a thread to stop what it is doing and terminate immediately is inherently unsafe, this is the reason why the Thread.stop method is deprecated:

    http://download.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html
  • 10. Re: ThreadPoolExecutor- Thread termination
    802316 Pro
    Currently Being Moderated
    Technically speaking, Thread.stop() doesn't terminate a thread. It causes it to throw a ThreadDeath Error where ever the code happens to be running ie. in a random place in your program. Its the randomness of the place and the possible state that makes it unsafe.

    However you can catch ThreadDeath and even ignore it, so there is no guarantee that Thread.stop() will terminate the thread.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points