ThreadWorks™ Sample Code

Table of Contents

 

Samples using TaskManager

import com.dvt.app.threadworks.TaskManager;

import com.dvt.app.threadworks.DefaultTaskManagerEnvironment;

 

/**

 * This class contains several example uses of the TaskManager.

 *

 * Copyright:  Delta Vortex Technologies, 2001.

 */

public class SamplesUsingTaskManager

{

 

  public static void main(String[] args)

  {

 

    try

    {

 

      /**

       * Sample definition of a TaskManager.  Typically, a TaskManager is

       * declared as "static" so that it is shared across the entire JVM,

       * EJB Container, RMI Service, CORBA service, or other type of application.

       *

       * TaskManager is Threadsafe.

       */

      DefaultTaskManagerEnvironment env = new DefaultTaskManagerEnvironment();

      env.setMaxNbrConcurrentTasks(5);

      env.setTaskTimeAllowedInMillis(4000);

      _taskManager = new TaskManager(env);

 

      //  Sample tasks for demo.

      SampleTask task1 = new SampleTask("Sample1");

      SampleTask task2 = new SampleTask("Sample2");

      SampleTask task3 = new SampleTask("Sample3");

      SampleTask task4 = new SampleTask("Sample4");

 

      // Simplest task execution.

      _taskManager.run(task1);

 

      // Task execution with completion notification

      SampleCompletionEventListener listener = new SampleCompletionEventListener();

      _taskManager.run(task2, listener);

 

      pause();

 

      // Setup for next demo.

      SampleTask[] firstTasks = new SampleTask[2];

      SampleTask[] lastTasks = new SampleTask[2];

      firstTasks[0] = task1;

      firstTasks[1] = task2;

      lastTasks[0] = task3;

      lastTasks[1] = task4;

 

      //  Run Task1 and Task2.  After they both complete, run Task3 and Task 4.

      //  Also, send a completion notification when they're all done.

      _taskManager.run(firstTasks, lastTasks, listener);

 

      pause();

      pause();

    }

    catch (Throwable t)  {t.printStackTrace();}

 

    System.exit(0);

 

  }

 

  private static void pause()

  {

    try {Thread.sleep(3000);}

    catch (Throwable t)  {t.printStackTrace();}

  }

 

  private static TaskManager            _taskManager = null;

}

 

Back to top

Samples using Scheduler

 

import com.dvt.app.threadworks.Scheduler;

import java.util.GregorianCalendar;

 

/**

 * This class contains several example uses of the Scheduler.

 *

 * Typically this kind of code will be placed where it's executed

 * when the application (or it's EJB Container, RMI Service, CORBA service,

 * or Servlet engine) is started.

 *

 * Copyright:  Delta Vortex Technologies, 2001.

 */

public class SamplesUsingScheduler

{

 

  public static void main(String[] args)

  {

 

    try

    {

      // Sample tasks for demo.

      SampleTask task1 = new SampleTask("Sample1");

      SampleTask task2 = new SampleTask("Sample2");

      SampleTask task3 = new SampleTask("Sample3");

 

      //  Sample task scheduled to run once in 5 ms.  I could have scheduled it

      //  to run in an hour or two.

      Scheduler.schedule(task1, 5);

 

      //  Sample task scheduled to run once at the current date/time.

      Scheduler.schedule(task2, new GregorianCalendar());

 

      SampleDependencyEventListener listener = new SampleDependencyEventListener();

 

      //  Sample task with custom dependencies scheduled to run once the current date/time.  I could

      //  have scheduled it to run in an hour or two.  It won't run because the dependencies

      //  in the sample listener are never meet.

      Scheduler.schedule(task3, new GregorianCalendar(), listener);

 

      /**

       * We'll wait two seconds and then make the listener dependencies = true.

       * You'll see task3 complete after we do that. Along with a completed notification

       * from the listener.

       */

      System.out.println("Waiting two seconds....");

      Thread.sleep(2000);

      System.out.println("Setting dependencies for Sample3 to true.");

      listener.setDependenciesMeet(true);

 

      Thread.sleep(4000);  // Wait for all tests to complete.

    }

    catch (Throwable t)  {t.printStackTrace();}

 

    System.exit(0);

  }

 

}

 

Back to top

 

 

© Copyright 2002, Delta Vortex Technologies, Inc., All rights reserved.

Last revised: April 28, 2002