Thread Monitoring
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 .
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 .
karim
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Bill,
but i want to restart a new thread when the thread stops by monitoring with some other thread.
but i want to restart a new thread when the thread stops by monitoring with some other thread.
karim
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You will need to create a new Thread and call its start method.
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
Steve
karimkhan pathan
Ranch Hand
Posts: 86
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks a lot Ajay ..
It is helpful.
It is helpful.
karim
Ajay Saxena
Ranch Hand
Posts: 154
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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 |











