• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Thread Monitoring

 
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 want a thread say first thread monitored by another thread say second thread .
When the first thread stops i want to restart the first thread with the help of second thread.
i.e second thread restarts the first one when it stops and the calls the run() methos of the first thread ...

please provide me some suggestions .
Thanks in advance .
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. You cant restart a Thread that has exited the run method.
2. Calling the run method is NOT the same as starting a new Thread.

A google search for "Java thread life cycle" will get you good reference material such as this page.

Bill
 
karimkhan pathan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill,

but i want to restart a new thread when the thread stops by monitoring with some other thread.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will need to create a new Thread and call its start method.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might also look at using ExecutorServices. You can either use a ThreadPoolExecutor to submit runnables (your task) to, receive a Future to check when the task is done, then resubmit it.

Or look up SceduledExecutorService, which may help in re-submitting the task for you (maybe using fixed interval of 0). That might save you some effort it programming your own monitor.
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Demo class :

 
karimkhan pathan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ajay ..
It is helpful.
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
np

Some possible improvements :

The code could be modified to monitor multiple worker threads,if required. And in the event there are frequent interruptions happening to worker threads (by way of setStop() being called in different threads), creating fresh threads to hand off the stopped worker (runnable) objects frequently is not a good idea. You would probably want to use a Thread pool to recycle the threads in which the stopped workers are restarted.

We would then have the following lines



replaced by



where the method runInThread() blocks till it finds a free thread whence it hands off the worker object to the latter.
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic