From e4012b2dc8297540c9501a10daa8b8746d55297e Mon Sep 17 00:00:00 2001 From: eea1 Date: Tue, 24 Aug 1999 23:10:15 +0000 Subject: Updated source files for Concurrency. --- java/JACE/Concurrency/ThreadManager.java | 113 +++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 java/JACE/Concurrency/ThreadManager.java (limited to 'java/JACE/Concurrency/ThreadManager.java') diff --git a/java/JACE/Concurrency/ThreadManager.java b/java/JACE/Concurrency/ThreadManager.java new file mode 100644 index 00000000000..d23e2410676 --- /dev/null +++ b/java/JACE/Concurrency/ThreadManager.java @@ -0,0 +1,113 @@ +/************************************************* + * + * = PACKAGE + * JACE.Concurrency + * + * = FILENAME + * ThreadManager.java + * + *@author Prashant Jain + * + *************************************************/ +package JACE.Concurrency; + +import java.util.*; +import JACE.OS.*; + +/** + * Wrapper for a ThreadGroup which provides additional methods for + * creating a certain number of Runnable instances. + */ +public class ThreadManager +{ + /** + * Default constructor + */ + public ThreadManager () + { + this (ACE.DEFAULT_THREAD_GROUP_NAME); + } + + /** + * Create a Thread Manager. + *@param groupName name of the thread group that the Thread Manager + * will manage + */ + public ThreadManager (String groupName) + { + this.thrGrp_ = new ThreadGroup (groupName); + if (this.thrGrp_ == null) + ACE.ERROR ("Thread group create failed"); + } + + /** + * Create a new thread. + *@param thr the caller whose run method will be invoked when the + * thread has been spawned + *@param daemon flag indicating whether the thread should be + * spawned off as a daemon thread + */ + public void spawn (Runnable thr, + boolean daemon) + { + Thread t = new Thread (this.thrGrp_, thr); + if (daemon) // Set the thread to be a daemon thread + t.setDaemon (true); + t.start (); + } + + /** + * Create a new thread and also give it a name. + *@param thr the caller whose run method will be invoked when the + * thread has been spawned + *@param threadName the name of the new thread + *@param daemon flag indicating whether the thread should be + * spawned off as a daemon thread + */ + public void spawn (Runnable thr, + String threadName, + boolean daemon) + { + Thread t = new Thread (this.thrGrp_, thr, threadName); + if (daemon) // Set the thread to be a daemon thread + t.setDaemon (true); + t.start (); + } + + + /** + * Create new threads. + *@param n the number of threads to spawn + *@param thr the caller whose run method will be invoked by each of + * the threads + *@param daemon flag indicating whether the threads should be + * spawned off as daemon threads + */ + public void spawnN (int n, + Runnable thr, + boolean daemon) + { + // Spawn off all the threads. + for (int i = 0; i < n; i++) + { + this.spawn (thr, daemon); + } + } + + /** + * Get the thread group containing all the threads. Note that the + * thread group can be used to get information regarding number of + * active threads as well as to suspend/resume all the threads in + * the group. + *@return the thread group that contains all the threads managed by + * the Thread Manager + */ + public ThreadGroup thrGrp () + { + return this.thrGrp_; + } + + private ThreadGroup thrGrp_; + // Thread Group that contains all the spawned threads + +} -- cgit v1.2.1