• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help I can't stop this thread then restart  RSS feed

 
Ronald Pruitt
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would appreciate if if someone could help me with this program. I have created a producer and consumer object as separate intities and have them communicating by one producing some text and the other consuming the text. I have read the items that I have been able to find in the saloon but thus far, they are of no help.
Is there a way to stop a thread without using the stop class. ie. stop the producer then tell the consumer that the producer has finished and thus it should stop too?
currently, the producer produces the current date and time, while the consumer sleeps for a brief amount of time, then the producer sleeps for a moment and so it goes. I can get it to stop using the stop() but that has been depricated and so I would prefer not doing so. I need to get this done ASAP, because my lab is due tomorrow. Any help would be appreciated.
 
Ipsita Naravane
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you tried destroy? Can someone tell me if destroy would work here?
Thanks
 
Matt Siegel
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the API, destroy() is not implemented . . . so it will do nothing.
Ron -- see what I wrote in the beginner's forum.
Matt
 
Don Smathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using jdk1.3, destroy() is supposed to work.
Otherwise, you might try putting each thread into its own threadgroup, then use destroy() on the group.
Let me know if this works ! Thanks !
 
Matt Siegel
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don -- In the API for 1.3, it says that destroy is not implemented. Also, in order to destroy a threadgroup it must be empty. Meaning that all threads in the group have already stopped.
Explicity killing a thread is a very dangerous thing to do. That is why these methods including Thread.stop() are deprecated. Read this link:
http://java.sun.com/j2se/1.3/docs/guide/misc/threadPrimitiveDeprecation.html
Matt
 
Don Smathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt --- you are correct !! After leaving this site, I played with some code using threads, and still have not found a means to explicitly kill ( stop ) a thread. So, I suspect there is also no way to stop a group -- MT or not; but this is a guess.
Could there be a way to employ finalize and gc to do this? If so, then I'm not sure one could re-start the thread --- what happens if one resets priorities -- may be same effect as stopping the thread; then just reassign a higher priority to restart it ???
 
Ipsita Naravane
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am coming across the same issue, and I can't seem to find a way to kill a thread :-(
I have a complex code, and to check a flag at all places would be an overkill. Any help would be appreciated.
 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can sometimes customize some control via the implementation of your run method. For instance if run() goes into a loop you can have each iteration check a condition variable and exit the run if true, etc.
 
James Arendt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I'm missing something, but couldn't you call interrupt() on the ThreadGroup and then have each thread handle it's own termination as appropriate?
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a FlagApplet to demo how to kill a thread w/o causing problems. See demo and its source code.
FlagApplet
BTW, dead Thread cannot be restarted. The above demo shows it too.
Thanks!
Roseanne
Join our SCJD Study Group when certified

[This message has been edited by Roseanne Zhang (edited May 13, 2001).]
 
Ipsita Naravane
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James, the interrupt() will work only on a thread that is sleeping or waiting, not on an active live thread
I also took a look at the source code provided by Roseanne.
While that looks really great there is one problem. I do not have exactly a loop, and To check a flag at each and every step(or steps of importence) would make the code look really ugly. I can't seem to find a way out
If only there was a way of throwing an exception in the run, it would have been so useful
any help would be greatly appreciated.
 
James Arendt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method interrupt() does in fact work on a running thread, it just doesn't throw an InterruptedException. Instead, you call either interrupted() or isInterrupted().
Sure, you're checking a "flag", but it's the only other solution. The simplest is to check something like isInterrupted() before you do any long processing or at time it seems most reasonable to make the check.
It might not be the prettiest, but that's all you have.
 
Ipsita Naravane
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if that's what I've got to do, then so be it
Thanks for all your help.
 
ANAND RAMKUMAR
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LET THE TWO THREAD WORK ON IN ONE PROCESS.THEN YOU CAN USE THE WAIT AND NOTIFY METHOD OF THE OBJECT RATHER THAN GOING FOR STOP().MOREOVER THIS STOPS DATACORRUPTION BECAUSE THE METHODS WILL INVOKED IN A SYNCHRONIZED METHOD.THIS ALSO WILL ALLOW TO DICARD ALL THE POLLING FOR TESTING THE STATE OF THE OBJECT.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!