• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Help Please!! I can't stop this thread

 
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 beendepricated 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.
thanx ron
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One simple way would to have a static boolean that the consumer checked everytime it went to read. And if the boolean changes then it should exit.
Example:

HTH!
Matt
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simplest would be to add a pleaseStop() method that sets a boolean member variable askedToStop to true. Then, instead of while(true) use while (!askedToStop).
More advanced would be to interrupt the consumer by calling interrupt() on its thread, and have the consumer check Thread.currentThread().isInterrupted() to tell it when to shut down.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic