当前位置:网站首页>Thread factory in thread pool

Thread factory in thread pool

2022-07-07 18:48:00 Gravel under Mount Everest

Since we all use thread pools , Then the work of creating threads is handed over to the thread pool . We just need to tell how many threads the thread pool needs , The rest of the work is left to the thread pool . Thread factories are used to create threads in the thread pool ,
Let's take a look at the thread factories :

ThreadFactory( Parent interface )

public interface ThreadFactory {
    

    //  Construct a new Thread . Implementations can also initialize priorities 、 name 、 Daemon status 、 ThreadGroup etc. 
    Thread newThread(Runnable r);
}

DefaultThreadFactory( By default )

//  Default thread factory 
static class DefaultThreadFactory implements ThreadFactory {
    
       //  Thread pool number 
        private static final AtomicInteger poolNumber = new AtomicInteger(1);
        //  Thread group 
        private final ThreadGroup group;
        //  Thread number 
        private final AtomicInteger threadNumber = new AtomicInteger(1);
        //  Thread name prefix 
        private final String namePrefix;
        
        DefaultThreadFactory() {
    
            SecurityManager s = System.getSecurityManager();
            //  Get thread groups 
            group = (s != null) ? s.getThreadGroup() :
                                  Thread.currentThread().getThreadGroup();
            //  Thread name prefix  = pool +  Thread pool number  + thread 
            namePrefix = "pool-" +
                          poolNumber.getAndIncrement() +
                         "-thread-";
        }
 
        //  Create a new thread 
        public Thread newThread(Runnable r) {
    
            //  Create thread ( Thread group , Mission , Thread name )
            Thread t = new Thread(group, r,
                                  namePrefix + threadNumber.getAndIncrement(),
                                  0);
            //  Determine whether the current query is a daemon 
            //  If it is a daemon thread, set it as a non daemon thread  
            if (t.isDaemon())
                t.setDaemon(false);
            //  Determine whether the priority of the thread is the default priority  
            if (t.getPriority() != Thread.NORM_PRIORITY)
                //  Set the thread priority to the default priority 
                t.setPriority(Thread.NORM_PRIORITY);
            return t;
        }
    }

PrivilegedThreadFactory

//  Privileged thread factory 
//  Thread factories capture access control contexts and class loaders 
static class PrivilegedThreadFactory extends DefaultThreadFactory {
    
        private final AccessControlContext acc;
        private final ClassLoader ccl;

        PrivilegedThreadFactory() {
    
            super();
            SecurityManager sm = System.getSecurityManager();
            if (sm != null) {
    
                // Calls to getContextClassLoader from this class
                // never trigger a security check, but we check
                // whether our callers have this permission anyways.
                sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);

                // Fail fast
                sm.checkPermission(new RuntimePermission("setContextClassLoader"));
            }
            this.acc = AccessController.getContext();
            this.ccl = Thread.currentThread().getContextClassLoader();
        }

        public Thread newThread(final Runnable r) {
    
            return super.newThread(new Runnable() {
    
                public void run() {
    
                    AccessController.doPrivileged(new PrivilegedAction<Void>() {
    
                        public Void run() {
    
                            Thread.currentThread().setContextClassLoader(ccl);
                            r.run();
                            return null;
                        }
                    }, acc);
                }
            });
        }
    }
原网站

版权声明
本文为[Gravel under Mount Everest]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071637121695.html