• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How to kill a thread with another thread

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Like
t1.interrupt(t2);
Sunetra?

------------------
azaman
 
Sunetra Saha
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, that's right.
 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thank You sunetra.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic