Package org.multiverse.utils
Class StandardThreadFactory
java.lang.Object
org.multiverse.utils.StandardThreadFactory
- All Implemented Interfaces:
ThreadFactory
A customizable implementation of the
ThreadFactory
. The new
java.util.concurrency library provides a ThreadFactory
interface, which is a great
thing, but strangely enough it doesn't provide an customizable implementation.
If the maximum priority of the ThreadGroup is changed after this StandardThreadFactory is
constructed, then this will be ignored by the StandardThreadFactory. So it could be that a
StandardThreadFactory has a higher priority than the ThreadGroup allowed. What will happen at
construction?-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final boolean
private final String
private static final AtomicInteger
private int
private final ThreadGroup
private final AtomicInteger
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new StandardThreadFactory with a Thread.NORM_PRIORITY as priority and a newly created ThreadGroup.StandardThreadFactory
(int priority) Constructs a new StandardThreadFactory with the given priority.StandardThreadFactory
(int priority, boolean daemon) Creates a new StandardThreadFactory with the given priority and if the threads are daemonsStandardThreadFactory
(int priority, String groupName) Constructs a new StandardThreadFactory with the given priority and with a newly created ThreadGroup with the given groupName.StandardThreadFactory
(int priority, ThreadGroup threadGroup) Constructs a new StandardThreadFactory with the given priority and are part of the give ThreadGroup.StandardThreadFactory
(int priority, ThreadGroup threadGroup, boolean daemon) Constructs a new StandardThreadFactory with the given priority and ThreadGroup.StandardThreadFactory
(String groupName) Constructs a new StandardThreadFactory with a Thread.NORM_PRIORITY as priority and with a newly created ThreadGroup with the given groupName. -
Method Summary
Modifier and TypeMethodDescriptionprivate static String
private void
ensureValidPriority
(int priority) int
Returns the priority of created Threads.Returns the ThreadGroup of the created Threads.boolean
Returns true if this StandardThreadFactory is producing daemon threads, false otherwise.void
setPriority
(int priority) Sets the priority of the threads.
-
Field Details
-
poolNumber
-
threadGroup
-
threadNumber
-
namePrefix
-
daemon
private final boolean daemon -
priority
private volatile int priority
-
-
Constructor Details
-
StandardThreadFactory
public StandardThreadFactory()Constructs a new StandardThreadFactory with a Thread.NORM_PRIORITY as priority and a newly created ThreadGroup. The created Threads are not daemons. -
StandardThreadFactory
Constructs a new StandardThreadFactory with a Thread.NORM_PRIORITY as priority and with a newly created ThreadGroup with the given groupName. The created threads are not daemons.- Parameters:
groupName
- the name of the ThreadGroup (is allowed to be null).
-
StandardThreadFactory
public StandardThreadFactory(int priority) Constructs a new StandardThreadFactory with the given priority. The created threads are not daemons.- Parameters:
priority
- the priority of th threads.- Throws:
IllegalArgumentException
- if the priority is not valid.
-
StandardThreadFactory
Constructs a new StandardThreadFactory with the given priority and with a newly created ThreadGroup with the given groupName. The created threads are not daemons.- Parameters:
priority
- the priority of the threads this StandardThreadFactory is going to createReference.groupName
- the name of the ThreadGroup (is allowed to be null).- Throws:
IllegalArgumentException
- if priority is not a valid value.
-
StandardThreadFactory
Constructs a new StandardThreadFactory with the given priority and are part of the give ThreadGroup. The created threads are not daemons.- Parameters:
priority
- the priority of the created threads.threadGroup
- the ThreadGroup the created Threads are part of.- Throws:
NullPointerException
- if threadGroup is nullIllegalArgumentException
- if the priority is not valid value.
-
StandardThreadFactory
public StandardThreadFactory(int priority, boolean daemon) Creates a new StandardThreadFactory with the given priority and if the threads are daemons- Parameters:
priority
- the priority of the thread.daemon
- if the thread is a daemon.
-
StandardThreadFactory
Constructs a new StandardThreadFactory with the given priority and ThreadGroup.- Parameters:
priority
- the priority of the threads this StandardThreadFactory is going to createReference.threadGroup
- the ThreadGroup the thread is part ofdaemon
- if the thread should be a daemon.- Throws:
IllegalArgumentException
- if the priority is not valid.NullPointerException
- if threadGroup is null.
-
-
Method Details
-
createThreadGroupName
-
ensureValidPriority
private void ensureValidPriority(int priority) -
isProducingDaemons
public boolean isProducingDaemons()Returns true if this StandardThreadFactory is producing daemon threads, false otherwise.- Returns:
- true if this StandardThreadFactory is producing daemon threads, false otherwise.
-
getThreadGroup
Returns the ThreadGroup of the created Threads.- Returns:
- the ThreadGroup of the created Threads.
-
getPriority
public int getPriority()Returns the priority of created Threads. This is a value ranging from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY.- Returns:
- the priority of created Threads.
-
setPriority
public void setPriority(int priority) Sets the priority of the threads. This will only effect newly created Threads. A value must be getAndSet ranging from Thread.MIN_PRIORITY and Thread.MAX_PRIORITY. This call is not completely threadsafe, the following scenario could happen:- thread1 call setPriority and newTransaction the checking part of this method and the check passes
- thread2 calls the ThreadGroup directly and lowers the priority
- thread1 sets the priority on this StandardThreadFactory
- Parameters:
priority
- the new priority.- Throws:
IllegalArgumentException
- if priority is smaller thanThread.MIN_PRIORITY
or larger thanThread.MAX_PRIORITY
or larger than the maximum priority of the ThreadGroup.
-
newThread
- Specified by:
newThread
in interfaceThreadFactory
-