aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread Monitoring a Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Monitoring a Thread" Watch "Thread Monitoring a Thread" New topic
Author

Thread Monitoring a Thread

cathy tho
Greenhorn

Joined: Jan 05, 2001
Posts: 1
Hi!
I'm looking for an example for a workerThread being monitored by another thread (monitorThread). The monitor's sole job is to restart(set isRunning=true) the workerThread should the workerThread's isRunning==false.
Anybody know of any?
BTW -- This is my first post -- I'm a Sun certified JP and have learned much from the discussions here -- Thanks
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
Cindy,
Welcome to the ranch. Thanks for the interesting workout; if this isn't what you're looking for then post back.<pre><code>
public class Sisyphus {
private Thread workerThread, monitorThread;
private Worker worker;
private Monitor monitor;
public Sisyphus() {
worker = new Worker();
workerThread = new Thread( worker );
monitor = new Monitor( worker );
monitorThread = new Thread( monitor );
workerThread.start();
monitorThread.start();
}
public static void main( String[] args ) {
Sisyphus me = new Sisyphus();
}
}
class Worker implements Runnable {
private boolean running;
public void run() {
while (!Thread.currentThread().interrupted()) {
if (running) {
synchronized (System.out) {
System.out.println( "Going to work now..." );
}
for (int i = 0; i < 10; i++) {
synchronized (System.out) {
System.out.println( "I'm working..." );
}
try {
Thread.currentThread().sleep( 500 );
} catch (InterruptedException ie) {
break;
}
}
synchronized (System.out) {
System.out.println( "Ahh, time to rest..." );
}
running = false;
}
}
}
public boolean isRunning() {
return running;
}
public void setRunning( boolean choice ) {
running = choice;
}
}
class Monitor implements Runnable {
private Worker worker;
public Monitor( Worker worker ) {
this.worker = worker;
}
public void run() {
while (!Thread.currentThread().interrupted()) {
if (!worker.isRunning()) {
synchronized (System.out) {
System.out.println( "Restarting that lazy worker..." );
}
worker.setRunning( true );
}
try {
Thread.currentThread().sleep( 1000 );
} catch (InterruptedException ie) {
break;
}
}
}
}</code></pre>
Jerry
P.S. Now that I look at it, the break in Worker.run() only breaks out of the for loop, not the while. I'll leave that for you to fix; I hate labeled breaks.
P.P.S. Run this with a performance monitor up and you'll see an ugly little spike every few seconds when the worker takes a break. The worker loop is spinning with no sleeps then, so if you use this you'll have to do something about that. A better solution would use wait and notify, but you specified the use of the flag variable.
[This message has been edited by Jerry Pulley (edited January 31, 2001).]
 
Don't get me started about those stupid light bulbs.
 
subject: Thread Monitoring a Thread