Class StandardThreadFactory

  • All Implemented Interfaces:
    java.util.concurrent.ThreadFactory

    public final class StandardThreadFactory
    extends java.lang.Object
    implements java.util.concurrent.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

      Fields 
      Modifier and Type Field Description
      private boolean daemon  
      private java.lang.String namePrefix  
      private static java.util.concurrent.atomic.AtomicInteger poolNumber  
      private int priority  
      private java.lang.ThreadGroup threadGroup  
      private java.util.concurrent.atomic.AtomicInteger threadNumber  
    • Constructor Summary

      Constructors 
      Constructor Description
      StandardThreadFactory()
      Constructs 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 daemons
      StandardThreadFactory​(int priority, java.lang.String groupName)
      Constructs a new StandardThreadFactory with the given priority and with a newly created ThreadGroup with the given groupName.
      StandardThreadFactory​(int priority, java.lang.ThreadGroup threadGroup)
      Constructs a new StandardThreadFactory with the given priority and are part of the give ThreadGroup.
      StandardThreadFactory​(int priority, java.lang.ThreadGroup threadGroup, boolean daemon)
      Constructs a new StandardThreadFactory with the given priority and ThreadGroup.
      StandardThreadFactory​(java.lang.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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.String createThreadGroupName()  
      private void ensureValidPriority​(int priority)  
      int getPriority()
      Returns the priority of created Threads.
      java.lang.ThreadGroup getThreadGroup()
      Returns the ThreadGroup of the created Threads.
      boolean isProducingDaemons()
      Returns true if this StandardThreadFactory is producing daemon threads, false otherwise.
      java.lang.Thread newThread​(java.lang.Runnable runnable)  
      void setPriority​(int priority)
      Sets the priority of the threads.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • poolNumber

        private static final java.util.concurrent.atomic.AtomicInteger poolNumber
      • threadGroup

        private final java.lang.ThreadGroup threadGroup
      • threadNumber

        private final java.util.concurrent.atomic.AtomicInteger threadNumber
      • namePrefix

        private final java.lang.String namePrefix
      • daemon

        private final boolean daemon
      • priority

        private volatile int priority
    • Constructor Detail

      • 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

        public StandardThreadFactory​(java.lang.String groupName)
        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:
        java.lang.IllegalArgumentException - if the priority is not valid.
      • StandardThreadFactory

        public StandardThreadFactory​(int priority,
                                     java.lang.String groupName)
        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:
        java.lang.IllegalArgumentException - if priority is not a valid value.
      • StandardThreadFactory

        public StandardThreadFactory​(int priority,
                                     java.lang.ThreadGroup threadGroup)
        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:
        java.lang.NullPointerException - if threadGroup is null
        java.lang.IllegalArgumentException - 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

        public StandardThreadFactory​(int priority,
                                     java.lang.ThreadGroup threadGroup,
                                     boolean daemon)
        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 of
        daemon - if the thread should be a daemon.
        Throws:
        java.lang.IllegalArgumentException - if the priority is not valid.
        java.lang.NullPointerException - if threadGroup is null.
    • Method Detail

      • createThreadGroupName

        private static java.lang.String 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

        public java.lang.ThreadGroup 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:

        1. thread1 call setPriority and newTransaction the checking part of this method and the check passes
        2. thread2 calls the ThreadGroup directly and lowers the priority
        3. thread1 sets the priority on this StandardThreadFactory
        The consequence is that the priority of this StandardThreadFactory is higher than the maximum priority of the ThreadGroup and this means that thread creation could fail because threads are created with a too high priority. This race problem is very hard to prevent because the check/getAndSet can't be done atomically because the ThreadGroup is exposed.
        Parameters:
        priority - the new priority.
        Throws:
        java.lang.IllegalArgumentException - if priority is smaller than Thread.MIN_PRIORITY or larger than Thread.MAX_PRIORITY or larger than the maximum priority of the ThreadGroup.
      • newThread

        public java.lang.Thread newThread​(java.lang.Runnable runnable)
        Specified by:
        newThread in interface java.util.concurrent.ThreadFactory