File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Cancelling a thread/task Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Cancelling a thread/task" Watch "Cancelling a thread/task" New topic
Author

Cancelling a thread/task

Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385


I am trying execute a http request in each thread. I would like to cancel a thread or a task, if it is not able to finish its execution in prescribed time. How do i do that.

I tried cancelling a task by using task.cancel. I found that the task is being cancelled. But the execution of the task still continues in the background.

thanks in advance
Shashank
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

You cannot stop a thread from execution. Read the "Why" at the API for Thread.stop().


Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
If the thread is in a thread pool, you even more must not stop() it, because it might not even be running the task you think it is running.

To stop an in-progress task, you have to code some sort of interruption protocol into the task. It could check a stop-flag every now and then, or maybe use Thread.interrupt() and Thread.interrupted() (though some people have told me off for that). If you use a stop-flag, make sure it is either accessed within a synchronized block or it is declared volatile.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can tell if one of your tasks didn't finish in time, even though you can't stop it. Your Future has a get() method with a timeout parameter. Start up a bunch of these things, then go back through the list and try to get the results.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
I wanted to assign some time for all threads to complete its operation. So there is a method at executor -- await...(1000). After the executor awaits, I iterate over all the tasks & call the get method like get(1000, TimeUnit.Millisecond). If it fails, it throws the exception. But still the task does'nt stop. I guess thats because, the thread calls an http request, the thread won't get stopped till the request returns. I probably have to set some kind of time out in the thread itself....
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

After the executor awaits, I iterate over all the tasks & call the get method like get(1000, TimeUnit.Millisecond). If it fails, it throws the exception. But still the task does'nt stop.

get method time out just indicates that the task is not completed after the wait period. It does not terminate the task.

I guess thats because, the thread calls an http request, the thread won't get stopped till the request returns.

Which library are you using to make HTTP requests?
You can set the socket timeout to some value if you are sure thats where the thread is hung for a long time. This will throw a SocketTimeOutException from the future and when you call get() method on the future, it willl throw the ExecutionException with the SocketTimeOutException as the cause.
[ July 12, 2007: Message edited by: Nitesh Kant ]

apigee, a better way to API!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cancelling a thread/task