• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How to Kill a running thread

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Need some help here. How do we kill a running thread after elapsing a settime say about 2 minutes?
I know we could use the Interrupt method of the thread to stop a running method but i am not sure how i can stop after a period of time.
Any help is appreciated.
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try checking out http://java.sun.com/docs/books/tutorial/essential/threads/timer.html
They added a Timer and TimerTask classes in 1.3 that may help
you. I haven't tried them - usually I grew my own by storing
the time when I entered my run() method and checked on each
iteration of a loop returning when I'd exceeded the desired time.
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
What you can do is check the current time by using System.currentTimeMillies() which returns a long. After this you can use the normal

you can check the System.currentTimeMillies() again in the while loop and interrupt the thread when the specified time has elapsed. If you do not get it i will write down the code.
You cannot use stop() however since it has been deprecated.


[This message has been edited by Rahul Mahindrakar (edited January 10, 2001).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rahul,
The <code>interrupt()</code> method is not guaranteed to stop the thread. The best way ( and what Sun recommends ) to kill a running thread is to use a state-based logic, like a flag or something that can be inspected in the run method. You will have to do this twice in your run method, once before entering into the method using a if-loop and then inside the method at regular intervals using some kind of polling logic.
Ajith
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith,
I agree with you when you say
interrupt() method is not guaranteed to stop the thread.
Thus one cannot "Kill" a thread once a specific period elapsed. However one can stop the work to be done safely as given in the code below

[This message has been edited by Rahul Mahindrakar (edited January 10, 2001).]
 
Hunter Dash
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the below code. now i want to stop an instance of runthread if the time exceeds a setlimit (in this case 2000 seconds)

when i run this program, Thread.currentThread().interrupt(); is not stopping the thread. what is the workaround?
/***************************
public class Runnner{
public Runnner() {
}
public static void main(String[] args) {
Runnner tea = new Runnner();
tea.run();
}

public void run() {
for (int i=0; i < 4; i++)
new runThread().start();
}

}
}

/*********************************************
import java.util.Timer;
import java.util.TimerTask;

public class runThread extends Thread {

Timer timer;
public runThread() {
//
}
class RemindTask extends TimerTask {
public void run() {
Thread.currentThread().interrupt();
timer.cancel(); //Terminate the timer thread
}
}
public void runCrawler() {
try {
//Start the timer
timer = new Timer();
timer.schedule(new RemindTask(), 2000);


runSomething() ; //This may take more than 2000 seconds
//If it exceeds i want to kill this
//thread not jvm.
} catch ( Exception e ) {
System.out.println( "Message: " + e.getMessage() );
e.printStackTrace();
}
}
public void run() {
try {
runCrawler();
} catch (Exception e) {
e.printStackTrace();
}
}
}
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
you have not enclosed your run method in a while loop which checks whether interrupt has been raized. Please refer to my code again especially this part.




[This message has been edited by Rahul Mahindrakar (edited January 10, 2001).]
 
Hunter Dash
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Rahul. But i must be missing some thing. take a look at the code in my previous posting.
The runSomething() method in the RunCrawler() method may take more than specified amount of time. I want to stop the thread when if it takes more than specified time.

Using the the new timerTask and timer classes combo i am able to get the the thread interrupted. But i am unable to stop the thread and it continues to execute.
 
reply
    Bookmark Topic Watch Topic
  • New Topic