001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }