• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Monitoring  RSS feed

 
karimkhan pathan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • 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 .
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need to create a new Thread and call its start method.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Demo class :

 
karimkhan pathan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Ajay ..
It is helpful.
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!