This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes How to kill runnig thread using java?(not waiting) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to kill runnig thread using java?(not waiting)" Watch "How to kill runnig thread using java?(not waiting)" New topic
Author

How to kill runnig thread using java?(not waiting)

P Chaudhari
Greenhorn

Joined: Mar 22, 2004
Posts: 22
Hi All,
I have implemented a Thread Pool. Thread of this thread pool executes execute() method of the difft. assignments given to it.
If thread gets stuck inside this execute method for certain amount of time, I want to kill it. I am able to know whether thread has completed this certain amount time but do not know how I can kill it safely as thread.stop() and suspend() are deprecated and calling interrupt() doesnot throw any exception.
If anybody can help?
Here is the code snippet..
public void run() {
Assignment assignment = null;
do {
assignment = null;
// waits here forever for assignment
assignment = threadPool.getAssignment();
if (assignment != null) {
assignment.execute();// If it stuck inside this execute() method??
}
} while (assignment != null);
}
TIA,
P.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

It depends on the definition of 'stuck.' If you can define the nature of stuckness, then we can examine some techniques to allow to break this condition.
In the end, it will be some form of the thread protecting itself from actually becoming fully halted. And when it is fully halted, it should be 'interruptable.'
P Chaudhari
Greenhorn

Joined: Mar 22, 2004
Posts: 22
Few more words on it..
ThreadPool runs all the difft. task which Implement Assignment interface. Assignment interface has execute() method. So what ever programmed inside execute, thread pool has no control over.
Now about "stucking of thread"
It can be anything, can be something like indefinite while loop inside execute of the Assignment. I can only check, it has been running since _certain_time_ , which is more than allowed limit. so should be killed.
regds,
P.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Unfortunately Threads are not stoppable like that. They only stop when they wish too. So its on the programmer to create kind threads that respect the programmers request.
In your case, you have no idea what code is running. That means you can not control the behavior of that thread. Therefore, that thread may not respect your 'request' for it to stop what its doing.
Further, the creator of the work being done could catch any thrown exceptions and continue to loop.
If this is critical, I believe you can start a process, and shut the whole process down when things go awry. RMI is probably best suited for this.
P Chaudhari
Greenhorn

Joined: Mar 22, 2004
Posts: 22
Thanks for the reply. I am also having same opinion. Only thing I am not allowed to bring down everything.
What about this? If all my Assignments are not having any common monitor. Then would it be safe to use stop() method? If there is any other argument against using stop() method.
regds,
P.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Come on now, lets not get desperate . Stopping Threads is a definite
If this is a safety issue then there is no question you want to start a 2nd process. That is the only way to ensure your main program can not be brought down. You need complete isolation. Else the new thread could eat up all the memory from your main program and crash it anyway. There are a host of things a thread could do to shut down your program. You definitely need a 2nd process. You can use RMI to start a working process. Maybe you need more control that RMI offers though since you will need to hold the Runtime on the process so you can shut it down if you need too. Probably you can use RMI for state comunication so you know its not dead.
P Chaudhari
Greenhorn

Joined: Mar 22, 2004
Posts: 22
Starting another process for threadpool is not feasible for us.
We have a web application and difft. large tasks in it were threads before I implemented ThreadPool. After ThreadPool implementation, I converted large tasks to Assignment. These Assignments get added to ThreadPool and threads of ThreadPool call Assignment.execute() inside their(threads) run(). If we start new process for ThreadPool, assignments will not be able to use the resources from main process and won't run. Also I do not want to bring down the entire ThreadPool for one stuck thread.
Is there any other way to kill single thread or I mis-understood your previous solution..
Please reply..
P.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

There is no way to stop an out of control thread. There is no way to prevent a thread from eating up all the resources from the process. You will need to start a 2nd process. What resources of the original process is it that you feel a 2nd process would not be able to access? You can communicate freely between the processes using RMI, so long as you make your data classes Serializable.
You can investigate SecurityManager which should have ways of limiting what other classes can do. It may be able to limit the thread from eating all your main programs resources.
You should revisit your position on using a new Process/Runtime if you need absolute control.
If you expect people to play nice and follow the rules, then use Thread.interrupt(), and catch the InterruptedException to end the thread.
[ March 29, 2004: Message edited by: CL Gilbert ]
Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
Could you describe what the execute() method does? Is it waiting for i/o response? waiting for a response from other thread?

can be something like indefinite while loop inside execute of the Assignment
You can create a flag. Whether the indefinite while loop runs is depended on the flag. When you want to stop running thread, use other thread to set the flag to false.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Joey Tran:
Could you describe what the execute() method does? Is it waiting for i/o response? waiting for a response from other thread?

can be something like indefinite while loop inside execute of the Assignment
You can create a flag. Whether the indefinite while loop runs is depended on the flag. When you want to stop running thread, use other thread to set the flag to false.

yes, the point is that it could be doing anything. he can not control what it does, nor is he aware. He only knows how long its been running.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to kill runnig thread using java?(not waiting)