HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Making Threads Flexible"
Vol. 3, Issue 6, P.50

	

Listing 1.

void timer(int interval) {

  long startTime = System.currentTimeMillis();

    while(true) {
      try {
            Thread.sleep(interval * 1000);
          }
       catch(InterruptedException ex) { }

        System.out.println(
          "The application has been running for " 
          + (int) ((System.currentTimeMillis() 
                  - startTime) / 1000) + " seconds");
      }
}

Listing 2.

public class AppTimer implements Runnable {

  int periodLength;

  public AppTimer(int newPeriodLength) {

    periodLength = newPeriodLength;
    Thread timer = new Thread(this);
    timer.setDaemon(true);
    timer.start();
  }

  public void run() { timer(periodLength); }

  // void timer(int) see Listing One

Listing 3.

public class AppTimer {

  class TimerHelper implements Runnable {

    AppTimer target;
    int      periodLength;

    TimerHelper(AppTimer newTarget, 
                int newPeriodLength) {

      target = newTarget;
      periodLength = newPeriodLength;
      Thread thread = new Thread(this);
      thread.setDaemon(true);
      thread.start();
    }

    public void run() { 

      target.timer(periodLength);
  }

  public AppTimer(int periodLength) {

    TimerHelper helper = 
             new TimerHelper(this, periodLength);
  }

  // void timer(int) see Listing 1
}

Listing 4.

import java.lang.reflect.*;

public class ThreadHelper implements Runnable {

  Object   targetObject;
  Method   targetMethod;
  Object[] parameters;
  Thread   thread;
  Object   result;
  boolean  completed = false;

  public ThreadHelper(Object   newTarget,
                      String   methodName,
                      Object[] newParameters) 
    throws java.lang.NoSuchMethodException {

    targetObject           = newTarget;
    parameters             = newParameters;
    Class[] parameterTypes = 
                   new Class[parameters.length];

    for (int i = 0; i < parameters.length; i++)
        parameterTypes[i] = 
                        parameters[i].getClass();

    targetMethod = targetObject.getClass().
                     getDeclaredMethod(
                     methodName, parameterTypes);

    thread = new Thread(this);
    thread.setDaemon(true);
    thread.start();
  }

  public void run() {

    try {
          result = targetMethod.invoke(
                       targetObject, parameters);
        }
    catch(InvocationTargetException ex) { 
                          System.err.println(ex);
    }
    catch(IllegalAccessException ex) { 
                          System.err.println(ex); 
    }
    signalCompletion(); 
  }

  synchronized void signalCompletion() {

    completed = true;
    notifyAll();
  }

  public synchronized Object join() {

    while (!completed) {
               try
               {
                 wait();
               }
               catch(InterruptedException ex) { }
    }
    return result;
  }
}

Listing 5.

public class AppTimer {

  public AppTimer(int periodLength) {

    Object[] parm = { new Integer(periodLength) };
    try {
        new ThreadHelper(this, "timer", parm);
    }
    catch(java.lang.NoSuchMethodException ex) { 
        System.err.println(ex);
    } 
  }

  // void timer(Integer) see Listing 1
}
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.