001package org.apache.commons.jcs3.utils.threadpool; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.concurrent.ThreadFactory; 023 024/** 025 * Allows us to set the daemon status on the threads. 026 */ 027public class DaemonThreadFactory 028 implements ThreadFactory 029{ 030 private final String prefix; 031 private final static boolean THREAD_IS_DAEMON = true; 032 private int threadPriority = Thread.NORM_PRIORITY; 033 034 /** 035 * Constructor 036 * 037 * @param prefix thread name prefix 038 */ 039 public DaemonThreadFactory(final String prefix) 040 { 041 this(prefix, Thread.NORM_PRIORITY); 042 } 043 044 /** 045 * Constructor 046 * 047 * @param prefix thread name prefix 048 * @param threadPriority set thread priority 049 */ 050 public DaemonThreadFactory(final String prefix, final int threadPriority) 051 { 052 this.prefix = prefix; 053 this.threadPriority = threadPriority; 054 } 055 056 /** 057 * Sets the thread to daemon. 058 * <p> 059 * @param runner 060 * @return a daemon thread 061 */ 062 @Override 063 public Thread newThread( final Runnable runner ) 064 { 065 final Thread t = new Thread( runner ); 066 final String oldName = t.getName(); 067 t.setName( prefix + oldName ); 068 t.setDaemon(THREAD_IS_DAEMON); 069 t.setPriority(threadPriority); 070 return t; 071 } 072}