public class CleaningDaemonThreadFactory
extends java.lang.Object
implements java.util.concurrent.ThreadFactory
cleanUp allows killing all threads.
A ThreadGroup is used to deal with all threads at a time. Its name is that given in the constructor.
Even though Josh Bloch tells us in "Effective Java", Item 53, that thread groups are almost useless,
we like the UncaughtExceptionHandler which JDK 1.4 only offers through ThreadGroups.
TODO: revisit this in JDK 1.5 where Thread itself has this handler.
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
LOG_THREAD_CREATION_CALLSTACK_PROPERTYNAME
If this property is set to
true, the creators of new threads will be identified through a INFO log showing the calling stack trace,
which helps for example in finding out who created a thread but later fails to clean it up
(see logs "Forcibly terminating surviving thread ..."). |
protected boolean |
logThreadCreationCallstack
Cache for value of
LOG_THREAD_CREATION_CALLSTACK_PROPERTYNAME. |
| Constructor and Description |
|---|
CleaningDaemonThreadFactory(java.lang.String name,
java.util.logging.Logger logger)
Normal constructor.
|
CleaningDaemonThreadFactory(java.lang.String name,
java.util.logging.Logger logger,
java.lang.String ownerName)
Special constructor, e.g.
|
| Modifier and Type | Method and Description |
|---|---|
java.util.List<java.lang.Thread> |
_getAllThreadsCreated()
Gets a copy of the list of all threads created by this factory up to this call.
|
void |
cleanUp()
Kills running threads via
Thread.interrupt() or Thread.stop(), see code comments about the two cases. |
java.lang.Thread |
newThread(java.lang.Runnable command)
Creates a new daemon thread that is part of the same factory thread group
as all other threads created by this method.
|
public static final java.lang.String LOG_THREAD_CREATION_CALLSTACK_PROPERTYNAME
true, the creators of new threads will be identified through a INFO log showing the calling stack trace,
which helps for example in finding out who created a thread but later fails to clean it up
(see logs "Forcibly terminating surviving thread ...").protected boolean logThreadCreationCallstack
LOG_THREAD_CREATION_CALLSTACK_PROPERTYNAME.public CleaningDaemonThreadFactory(java.lang.String name,
java.util.logging.Logger logger)
name - the name of the ThreadGroup to which all threads created by this factory will belong.logger - the logger to be used by this classpublic CleaningDaemonThreadFactory(java.lang.String name,
java.util.logging.Logger logger,
java.lang.String ownerName)
name - the name of the ThreadGroup to which all threads created by this factory will belong.logger - the logger to be used by this classownerName - Can show up in log messages such as warnings by LoggingThreadGroup#uncaughtException(Thread, Throwable).public java.lang.Thread newThread(java.lang.Runnable command)
newThread in interface java.util.concurrent.ThreadFactoryThreadFactory.newThread(java.lang.Runnable)public java.util.List<java.lang.Thread> _getAllThreadsCreated()
public void cleanUp()
Thread.interrupt() or Thread.stop(), see code comments about the two cases.
Should be called by the container or similar classes when all threads
created by this factory supposedly have terminated anyway thanks to smart applications.
The safety concerns which led to the deprecation of the stop method thus don't seem to apply here.