Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cancelling a thread/task

 
Nikhil Jain
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot stop a thread from execution. Read the "Why" at the API for Thread.stop().
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nikhil Jain
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic