Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to kill a thread with another thread  RSS feed

 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a problem here like
If there are 2 threads t1 and t2 both running in a program and my task is t2 should kill t1 and stop running. Can anyone explain this?
Thanks in advance.
Madhuri.
 
Sunetra Saha
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the stop method is deprecated, I guess that the best thing you can do is send an interrupt to the thread t2 from t1. t2's run can work while !interrupted.
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Like
t1.interrupt(t2);
Sunetra?

------------------
azaman
 
Sunetra Saha
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that's right.
 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thank You sunetra.
 
Mo Ibrahim
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The interrupt() call is not guaranteed to work. If only t1 is on a wait() call, t2 can call interrupt() to do what you want to do.
I think there is a better alternative to achieve this. Use a common resource that both threads check. t2 can set the proper condition on which t1 can return and t2 can also check the same value and return.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that common resource that one thread can set and the other one check -- that's the interrupted flag. No use reinventing the wheel. Cf Thread.interrupted().
- Peter
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!