Java ThreadFactory createDaemonThreadFactory(final int threadPriority, final ThreadGroup group, final ClassLoader contextClassLoader)

Here you can find the source of createDaemonThreadFactory(final int threadPriority, final ThreadGroup group, final ClassLoader contextClassLoader)

Description

Creates a new instance of the thread factory with given thread instantiation parameters.

License

Open Source License

Parameter

Parameter Description
threadPriority The priority of all threads which created by returned factory.
group Thread group for all threads which created by returned factory. May be null .
contextClassLoader The context class loader used by all threads which created by returned factory. May be null .

Return

A new instance of the thread factory.

Declaration

public static ThreadFactory createDaemonThreadFactory(final int threadPriority, final ThreadGroup group,
        final ClassLoader contextClassLoader) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.concurrent.*;

public class Main {
    /**//from w w w.j a  va  2 s  .  c o m
     * Creates a new instance of the thread factory with given thread instantiation parameters.
     * <p>
     *     The returned factory always creates daemon threads. For more information, see {@link Thread#setDaemon(boolean)}.
     * </p>
     * @param threadPriority The priority of all threads which created by returned factory.
     * @param group Thread group for all threads which created by returned factory. May be {@literal null}.
     * @param contextClassLoader The context class loader used by all threads which created by returned factory. May be {@literal null}.
     * @return A new instance of the thread factory.
     */
    public static ThreadFactory createDaemonThreadFactory(final int threadPriority, final ThreadGroup group,
            final ClassLoader contextClassLoader) {
        return r -> {
            final Thread t = new Thread(group, r);
            t.setDaemon(true);
            t.setPriority(threadPriority);
            t.setContextClassLoader(contextClassLoader);
            return t;
        };
    }

    /**
     * Creates a new instance of the thread factory with given thread instantiation parameters.
     *  <p>
     *     The returned factory always creates daemon threads. For more information, see {@link Thread#setDaemon(boolean)}.
     * </p>
     * @param threadPriority The priority of all threads which created by returned factory.
     * @param group Thread group for all threads which created by returned factory. May be {@literal null}.
     * @return A new instance of the thread factory.
     */
    public static ThreadFactory createDaemonThreadFactory(final int threadPriority, final ThreadGroup group) {
        return createDaemonThreadFactory(threadPriority, group, Thread.currentThread().getContextClassLoader());
    }
}

Related

  1. createFactory(final String threadName)
  2. createNamedThreadFactory(final String threadName)
  3. createSimpleThreadFactory(final String name)
  4. createThreadFactory(final String threadName, final int stackSize)