Adverts

This class doesn't fit very well in the concurrency section but it fits even worse in the other sections. What it aims to show is a way of putting together an application that consists of a number of daemon worker threads. The problem with having an application that consists only of daemon threads is that as soon at the main thread has finished starting them the application will exit. The code below shows one way to ensure that the application continues running until such time as it is asked to shut down. The example is a little contrived, the worker thread drops through after three iterations of the loop what ever happens. Imagine, however, a situation where the worker is waiting for a message from a socket or the console requesting that the application shut down and until it receives that message it keeps going.

This first example uses a busy wait via sleep to poll for the shut down flag. If you want to use wait and notify instead see the second example below.

package example;

public class HookTestSleep {

 
private volatile boolean shutdown = false;
 
 
private HookTestSleep() {   
   
Runtime.getRuntime().addShutdownHook( new Thread() {
           
public void run() {
               
System.out.println( "In shutdown hook thread" );
           
}
        })
;
   
    Worker worker =
new Worker();
    worker.setDaemon
( true );
    worker.start
();
   
   
try {
     
synchronized (this) {
       
while( !shutdown ) {
         
System.out.println( "Waiting..." );
          Thread.currentThread
().sleep( 2000 );
       
}
      }
     
System.out.println( "Woken up." );
   
} catch (InterruptedException e) {
     
e.printStackTrace();
   
}
  }
 
 
public void otherMethod() {
   
System.out.println( "In other method" );
 
}
 
 
/**
   *
@param args
   */
 
public static void main(String[] args) {
   
System.out.println( "Starting..." );
   
new HookTestSleep();       
    System.out.println
( "Exiting..." );
 
}

 
private class Worker extends Thread {   
   
public void run() {
     
int count = 0;
     
while( count < ) {
       
System.out.println( "Worker running." );
        otherMethod
();
        count++;
       
try {
         
Thread.currentThread().sleep( 2000 );
       
} catch (InterruptedException e) {
         
e.printStackTrace();
       
}
      }
     
     
shutdown = true;
   
}
  }
}

This second example uses wait and notify to cause the main thread to exit. This is a more efficient way of causing the termination as it doesn't use a busy wait.

package example;

public class HookTest {

 
private volatile boolean shutdown = false;
 
private Object parent;
 
 
private HookTest() {
   
parent = this;
   
    Runtime.getRuntime
().addShutdownHook( new Thread() {
           
public void run() {
               
System.out.println( "In shutdown hook thread" );
           
}
        })
;
   
    Worker worker =
new Worker();
    worker.setDaemon
( true );
    worker.start
();
   
   
try {
     
synchronized (this) {
       
while( !shutdown ) {
         
System.out.println( "Waiting..." );
          wait
();
       
}
      }
     
System.out.println( "Woken up." );
   
} catch (InterruptedException e) {
     
e.printStackTrace();
   
}
  }
 
 
public void shutdown() {
   
shutdown = true;
   
   
synchronized (parent) {
     
parent.notify();
   
}
  }
 
 
public void otherMethod() {
   
System.out.println( "In other method" );
 
}
 
 
/**
   *
@param args
   */
 
public static void main(String[] args) {
   
System.out.println( "Starting..." );
   
new HookTest();       
    System.out.println
( "Exiting..." );
 
}

 
private class Worker extends Thread {   
   
public void run() {
     
int count = 0;
     
while( count < ) {
       
System.out.println( "Worker running." );
        otherMethod
();
        count++;
       
try {
         
Thread.currentThread().sleep( 2000 );
       
} catch (InterruptedException e) {
         
e.printStackTrace();
       
}
      }
     
     
shutdown();
   
}
  }
}

Adverts

Donate and Help

Please support this site and
Bandwidth doesn't grow on trees y' know :o)

Adverts

Get Adsense