wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes MultiThreaded Programming: Killing Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "MultiThreaded Programming: Killing Thread" Watch "MultiThreaded Programming: Killing Thread" New topic
Author

MultiThreaded Programming: Killing Thread

Pash Sonar
Greenhorn

Joined: May 25, 2005
Posts: 7
I have encountered a problem while implementing the following requirement,

My requirement:- I have two threads Thread_1 and Thread_2.
Thread_2 performs TASK which can�t be implemented or performed in loop as it is not a single functionality to achieve.
Thread_1 should start the Thread_2 and Thread_2 needs to be stopped after a pre-defined time, say �5 minutes� only in case of Thread_2 has not completed the TASK that it was executing.

I am trying to meet a requirement by following approach

class Thread_2 extends Thread
{
public void run(){
---
---
---
//TASK, tight bound. here for() = =TASK
for (int i=0;i<=100000;i++ )
{
// out of my scope to handle here
}
//end of TASK
-----
}
}

class Thread_1 extends XYZ
//should extend; XYZ implements java.lang.Runnable
{
-----
Public viod run(){
Therad_2 obj2 = new Therad_2();
------
th.start();
// Here is the PROBLEM. I have to stop the thread �obj2� after some �X� time means obj2 thread should run only for �X� time. In short I got to kill the thred.
}// end run()

}// end Thread_1

I would appreciate if any body can have a look.
Yaroslav Chinskiy
Ranch Hand

Joined: Jan 09, 2001
Posts: 147
Why dont u create a Runnable with following method:

volatile boolean run = true;

run(){
for (int i=0;i<=100000 && run;i++ )
{

}
}

abort(){
run = false;
}

Also, if the operation is the loop is IO blocked, u have to interrupt the thread. for this u can cache reference to the thrad from run method and then interrupt it in the abort method.

Thread t;

run(){
t = currentThread();
for(){
}
}

abort(){
run = false;
if(t!= null && t.isAlive()){
t.interruprt();
}

Hope that helps.
Sameer Amte
Ranch Hand

Joined: Oct 22, 2002
Posts: 38
Calling interrupt() will not help if the "TASK" method is blocking for any reason. As per the interrupt documentaion, calling interrupt() will only cause the interrupted flag to be set if the method is blocking.
Yaroslav Chinskiy
Ranch Hand

Joined: Jan 09, 2001
Posts: 147
Sameer Amte,

I am looking at the java docs:

this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.

If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

So any IO or wait or sleep will be interrupted. That is why you put try/catch around wait()/sleep().

If one could not interrupt blocking IO then interrupt() whould not make any sense.
Sameer Amte
Ranch Hand

Joined: Oct 22, 2002
Posts: 38
The documentation mentions only about nio classes that will be interrupted.If a thread is in blocking IO or is in a continuously running loop that does not check Thread.isInterrupted(), it will not get interrupted.
I have tested this in various ways and calling interrupt on a thread does not force it to come out of a blocking IO call or out of a continuously running loop.
If your "TASK" is continuously polling then you might want to check isInterrupted() after a call to your "TASK" method.
Yaroslav Chinskiy
Ranch Hand

Joined: Jan 09, 2001
Posts: 147
Sameer,

Looks like I was wrong in interpretation of the docs.
Simple program proves ur point.


Thanks.
 
 
subject: MultiThreaded Programming: Killing Thread