001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.concurrent.ThreadFactory;
018    
019    /**
020     * @author Michael C. Han
021     */
022    public class NamedThreadFactory implements ThreadFactory {
023    
024            public NamedThreadFactory(
025                    String name, int priority, ClassLoader contextClassLoader) {
026    
027                    SecurityManager securityManager = System.getSecurityManager();
028    
029                    if (securityManager != null) {
030                            _group = securityManager.getThreadGroup();
031                    }
032                    else {
033                            Thread currentThread = Thread.currentThread();
034    
035                            _group = currentThread.getThreadGroup();
036                    }
037    
038                    _name = name;
039                    _priority = priority;
040                    _contextClassLoader = contextClassLoader;
041            }
042    
043            public Thread newThread(Runnable runnable) {
044                    Thread thread = new Thread(_group, runnable, _name);
045    
046                    thread.setDaemon(true);
047                    thread.setPriority(_priority);
048    
049                    if (_contextClassLoader != null) {
050                            thread.setContextClassLoader(_contextClassLoader);
051                    }
052    
053                    return thread;
054            }
055    
056            private ClassLoader _contextClassLoader;
057            private ThreadGroup _group;
058            private String _name;
059            private int _priority;
060    
061    }