I start couple of threads, but I do not have their references to stop by signalling or something.
For example, I can't pass a variable like running=false
to those threads, because I do not have their references, but have their names.
I'm using a ThreadGroup
and always I have the reference of it. So I can do something like this. Seems to be it doesn't work.
Thread[] threads = new Thread[threadGroup.activeCount()];
int count = threadGroup.enumerate(threads);
for(int i = 0; i < count; i++){
threads[i].interrupt();
}
This is a sample of my thread.
public void run{
try{
//myDAO.getRecords();
//this takes 30seconds to 60
//returns about 3 millions of records
}catch(Exception e){
//log
}
}
When this thread is executing, I want to stop it in the middle.
Anyway batabase query is running, but I want to stop getting results.
Still I'm getting results even I call interrupt()
.
Are there any other ways to do this OR have I done anything wrong ? Ultimately the task is to cancel a long running sql query from Java.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…