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();
}
}
}