public class ThreadManager
extends java.lang.Object
import de.bb.util.*; public class TmTest { static class F implements ThreadManager.Factory { public void create(ThreadManager tm) { new T(tm).start(); // create the thread and start the thread } } static class T extends ThreadManager.Thread { T(ThreadManager tm) { super(tm); } public void run() { // this endless loop is a MUST while (!mustDie()) { try { // wait for trigger int t = 1000 + (int) (Math.random() * 2000); System.out.println("sleeping : " + t); sleep(t); // now start to do something setBusy(); // checks also whether threads must be added // simulate work t = 1000 + (int) (Math.random() * 2000); System.out.println("working : " + t); sleep(t); } catch (Throwable t) // you also MUST catch EVERYTHING! { } } System.out.println("dead ++++++++++++"); } } // example main public static void main(String args[]) { ThreadManager tm = new ThreadManager(new F()); tm.setMaxCount(20); try { tm.setWaitCount(3); System.out.println("3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 "); Thread.sleep(10 * 1000); tm.setWaitCount(10); System.out.println("10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 "); Thread.sleep(10 * 1000); tm.setWaitCount(5); System.out.println("5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5"); Thread.sleep(10 * 1000); tm.setMaxCount(10); System.out.println("m10 m10 m10 m10 m10 m10 m10 m10 m10 m10 m10 m10 m10 m10 m10 m10 m10 m10"); Thread.sleep(10 * 1000); tm.setMaxCount(0); System.out.println("0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "); while (tm.size() > 0) Thread.sleep(1000); } catch (Exception e) { } } }
Modifier and Type | Class and Description |
---|---|
static interface |
ThreadManager.Factory
Interface which contains the thread creating function.
|
static class |
ThreadManager.Thread
Abstract class for all threads used with the ThreadManager.
|
Constructor and Description |
---|
ThreadManager(java.lang.String name,
ThreadManager.Factory f)
Create a ThreadManager object.
|
ThreadManager(ThreadManager.Factory f)
Create a ThreadManager object.
|
Modifier and Type | Method and Description |
---|---|
int |
getMaxCount()
Query many threads are maximal available.
|
int |
getRunning()
Return the count of running threads.
|
void |
renew()
Replace each running thread with a new thread.
|
void |
setMaxCount(int max)
Defines how many threads are maximal available.
|
void |
setWaitCount(int wc)
Defines how many threads are waiting.
|
int |
size()
Get total count of threads.
|
public ThreadManager(ThreadManager.Factory f)
f
- a factory to create the managed threads.public ThreadManager(java.lang.String name, ThreadManager.Factory f)
name
- a name shown in debug thread view.f
- a factory to create the managed threads.public void setWaitCount(int wc)
wc
- set the count of waiting threadspublic void setMaxCount(int max)
max
- set the max count of threadsgetMaxCount()
public int getMaxCount()
setMaxCount(int)
public int size()
public void renew()
public int getRunning()