• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use threads to "stop" the execution of a method without a "return"?  RSS feed

 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I had code like this:



Is it possible that by using Thread I can have otherObject abbruptly terminate whatever exampleMethod() was about to run?

I realise there are other ways of doing this sort of thing... I already have it implemented by just calling "return" but it's less solid than I want it to be because I really want otherObject to be then one which ends the execution.

I've never actually done anything with threads and reading around the API and some tutorials I see mentions of Thread.stop() (deprecated) but nothing similar to what I'm after here.

Cheers,

Chris
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search this forum, as this is one of the very most frequently asked questions.

The quick answer is no, you cannot forcibly stop another thread. The Thread.stop() method tried to offer that facility, but was quickly realised to be a completely broken idea; it must NOT be used.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than using threads, otherObject's someNotification() method could simply throw an exception. Whether or not that makes sense may depend on whether you regard the situation as normal, or exceptional. But ignoring that debate about the philosophy of when to use exceptions, it's certainly possible to use them here. It's what I would probably do.

Another option is to have a boolean instance variable - let's call it isCancelled. Then you can rewrite your code as

or

and then the someNotification() method needs to be able to set isCancelled() to true somehow. This strategy probably requires more coding on your part than the exception does.
[ September 03, 2007: Message edited by: Jim Yingst ]
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had an implementation which threw an instance of Throwable (I didn't want to extend "Exception" for the fact it's not an exceptional situation, it's just for program flow. I started a thread on here last week as to whether or not that was a reasonable thing to do and people seemed to be of the opinion that Throwable instances should never be used outside of exceptional situations.

I had:



Maybe I'll go back to this because it worked a peach
[ September 04, 2007: Message edited by: Chris Corbyn ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, doing something like that is certainly unusual, and outside of most people's paradigms. But I wouldn't rule it out. Be aware that it's the sort of thing that will meet resistance from people, as they've been told it's Wrong. You may be interested in this discussion which references this article. If the performance of the exception throwing is a problem, you can improve it considerably - at the cost of valuable debugging information. If I were to do such a thing, I'd probably make it so the stack trace can be enabled or disabled at runtime - probably tied to the logging level. E.g. using Log4J:

Returning this rather than null seems appropriate to better fulfill the contract of the method. It seems to make no difference when using the current JDK, but returning null could cause a NullPointerException if they rewrite some of the code that uses fillInStackTrace() in the future - why risk it?
[ September 04, 2007: Message edited by: Jim Yingst ]
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would argue that causing a method to exit abnormally is be definition an exceptional situation. return is so much more graceful.

The exception will not only stop the method, but it will stop the whole thread if it is not caught.
[ September 09, 2007: Message edited by: Mr. C Lamont Gilbert ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!