Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Monitoring a Thread

 
cathy tho
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic