|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectde.bb.util.ThreadManager
public class ThreadManager
Used to manage threads for the same task, when the count of needed tasks changes with the time.
A good example is a HTTP server where each request gets an own task to process the request and computes the output.
It's easy understandable that the count of needed threads depends on the incoming requests, the duration of the processing and so on.
The complete management is done by this class, only the count of waiting threads, ready to catch the next incoming request, and a maximum count of active threads, to limit and protect from overload must be configured.
An important hint is: Donīt use more waiting threads for one task than available CPU.
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)
{
}
}
}
| Nested Class Summary | |
|---|---|
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 Summary | |
|---|---|
ThreadManager(java.lang.String name,
ThreadManager.Factory f)
Create a ThreadManager object. |
|
ThreadManager(ThreadManager.Factory f)
Create a ThreadManager object. |
|
| Method Summary | |
|---|---|
int |
getMaxCount()
Query many threads are maximal available. |
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 started. |
int |
size()
Get total count of threads. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
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.| Method Detail |
|---|
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()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||