KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > CustomizableThreadCreator


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util;
18
19 /**
20  * Simple customizable helper class for creating threads. Provides various
21  * bean properties, such as thread name prefix, thread priority, etc.
22  *
23  * <p>Serves as base class for thread factories such as
24  * {@link org.springframework.scheduling.concurrent.CustomizableThreadFactory}.
25  *
26  * @author Juergen Hoeller
27  * @since 2.0.3
28  * @see org.springframework.scheduling.concurrent.CustomizableThreadFactory
29  */

30 public class CustomizableThreadCreator {
31
32     private final Object JavaDoc monitor = new Object JavaDoc();
33
34     private String JavaDoc threadNamePrefix;
35
36     private int threadPriority = Thread.NORM_PRIORITY;
37
38     private boolean daemon = false;
39
40     private ThreadGroup JavaDoc threadGroup;
41
42     private int threadCount = 0;
43
44
45     /**
46      * Create a new CustomizableThreadCreator with default thread name prefix.
47      */

48     public CustomizableThreadCreator() {
49         this(null);
50     }
51
52     /**
53      * Create a new CustomizableThreadCreator with the given thread name prefix.
54      * @param threadNamePrefix the prefix to use for the names of newly created threads
55      */

56     public CustomizableThreadCreator(String JavaDoc threadNamePrefix) {
57         setThreadNamePrefix(threadNamePrefix);
58     }
59
60
61     /**
62      * Specify the prefix to use for the names of newly created threads.
63      * Default is "SimpleAsyncTaskExecutor-".
64      */

65     public void setThreadNamePrefix(String JavaDoc threadNamePrefix) {
66         this.threadNamePrefix = (threadNamePrefix != null ? threadNamePrefix : getDefaultThreadNamePrefix());
67     }
68
69     /**
70      * Return the thread name prefix to use for the names of newly
71      * created threads.
72      */

73     public String JavaDoc getThreadNamePrefix() {
74         return this.threadNamePrefix;
75     }
76
77     /**
78      * Set the priority of the threads that this factory creates.
79      * Default is 5.
80      * @see java.lang.Thread#NORM_PRIORITY
81      */

82     public void setThreadPriority(int threadPriority) {
83         this.threadPriority = threadPriority;
84     }
85
86     /**
87      * Return the priority of the threads that this factory creates.
88      */

89     public int getThreadPriority() {
90         return this.threadPriority;
91     }
92
93     /**
94      * Set whether this factory is supposed to create daemon threads,
95      * just executing as long as the application itself is running.
96      * <p>Default is "false": Concrete factories usually support explicit
97      * cancelling. Hence, if the application shuts down, Runnables will
98      * by default finish their execution.
99      * <p>Specify "true" for eager shutdown of threads which still
100      * actively execute a Runnable.
101      * @see java.lang.Thread#setDaemon
102      */

103     public void setDaemon(boolean daemon) {
104         this.daemon = daemon;
105     }
106
107     /**
108      * Return whether this factory should create daemon threads.
109      */

110     public boolean isDaemon() {
111         return this.daemon;
112     }
113
114     /**
115      * Specify the name of the thread group that threads should be created in.
116      * @see #setThreadGroup
117      */

118     public void setThreadGroupName(String JavaDoc name) {
119         this.threadGroup = new ThreadGroup JavaDoc(name);
120     }
121
122     /**
123      * Specify the thread group that threads should be created in.
124      * @see #setThreadGroupName
125      */

126     public void setThreadGroup(ThreadGroup JavaDoc threadGroup) {
127         this.threadGroup = threadGroup;
128     }
129
130     /**
131      * Return the thread group that threads should be created in
132      * (or <code>null</code>) for the default group.
133      */

134     public ThreadGroup JavaDoc getThreadGroup() {
135         return this.threadGroup;
136     }
137
138
139     /**
140      * Template method for the creation of a Thread.
141      * <p>Default implementation creates a new Thread for the given
142      * Runnable, applying an appropriate thread name.
143      * @param runnable the Runnable to execute
144      * @see #nextThreadName()
145      */

146     public Thread JavaDoc createThread(Runnable JavaDoc runnable) {
147         Thread JavaDoc thread = new Thread JavaDoc(getThreadGroup(), runnable, nextThreadName());
148         thread.setPriority(getThreadPriority());
149         thread.setDaemon(isDaemon());
150         return thread;
151     }
152
153     /**
154      * Return the thread name to use for a newly created thread.
155      * <p>Default implementation returns the specified thread name prefix
156      * with an increasing thread count appended: for example,
157      * "SimpleAsyncTaskExecutor-0".
158      * @see #getThreadNamePrefix()
159      */

160     protected String JavaDoc nextThreadName() {
161         int threadNumber = 0;
162         synchronized (this.monitor) {
163             this.threadCount++;
164             threadNumber = this.threadCount;
165         }
166         return getThreadNamePrefix() + threadNumber;
167     }
168
169     /**
170      * Build the default thread name prefix for this factory.
171      * @return the default thread name prefix (never <code>null</code>)
172      */

173     protected String JavaDoc getDefaultThreadNamePrefix() {
174         return ClassUtils.getShortName(getClass()) + "-";
175     }
176
177 }
178
Popular Tags