• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stopping running threads: how to ?

 
Claude Moore
Ranch Hand
Posts: 832
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I was searching a way to stop forcibly a running thread whose run method is a endless loop. Not surprisingly, it turned out that there's no way to stop "in a general way" any running thread, and there's a lot of good reasons explained. I hoped that cancel(true) method invoked against a future instance should have made the trick, but I was wrong. So i need to clarify my ideas, and I'd like to get your help.

Generally speaking, may be correct to state that a Runnable, a Callable or a Thread class may be interrupted while executing its run method only if in run method is present any instruction which may raise an InterruptedException or that suspends thread execution putting it in a waiting or suspended state ?

If that's true, I would like to know if there's any pattern to follow to design threads that are easily stoppable. The only thing that came in my mind was to design a simple class hierarchy in which base class looks like the following one:



so, for all extended classes, one should need only to code doBusinessWork in such a way to invoke checkInterruptionRequired every time an endless or very long-lasting loop may be encountered.

What do you think about ? I suppose that there are better approaches..

Thanks in advance.



 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claude Moore wrote:
Generally speaking, may be correct to state that a Runnable, a Callable or a Thread class may be interrupted while executing its run method only if in run method is present any instruction which may raise an InterruptedException or that suspends thread execution putting it in a waiting or suspended state ?



I think that is right. If the callable is already in the running state, a cancel(true) has no affect if the callable does not support its own interruption. And I think what you've shown is a nice way of interrupting already executing threads. A callable can periodically check if it should interrupt and hence if it should exit.

Disclaimer : I don't know if there is any other nice way of stopping a running thread.
 
Claude Moore
Ranch Hand
Posts: 832
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply.
Has anyone else any other ideas on this topic ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35719
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ar correct that your thread needs to check periodically if it should stop.
 
Claude Moore
Ranch Hand
Posts: 832
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for your answers guys.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!