01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.thread;
18:
19: /**
20: * This class is responsible to create new Thread instances to run a command.
21: *
22: * @author <a href="mailto:info@otego.com">Otego AG, Switzerland</a>
23: * @version $Id: DefaultThreadFactory.java 56765 2004-11-06 13:54:31Z giacomo $
24: */
25: public class DefaultThreadFactory implements ThreadFactory,
26: EDU.oswego.cs.dl.util.concurrent.ThreadFactory {
27: //~ Instance fields --------------------------------------------------------
28:
29: /** The daemon mode */
30: private boolean m_isDaemon = false;
31:
32: /** The priority of newly created Threads */
33: private int m_priority = Thread.NORM_PRIORITY;
34:
35: //~ Methods ----------------------------------------------------------------
36:
37: /**
38: * Set the isDaemon property
39: *
40: * @param isDaemon Whether or not new <code>Thread</code> should run as
41: * daemons.
42: */
43: public void setDaemon(boolean isDaemon) {
44: m_isDaemon = isDaemon;
45: }
46:
47: /**
48: * Get the isDaemon property
49: *
50: * @return Whether or not new <code>Thread</code> will run as daemons.
51: */
52: public boolean isDaemon() {
53: return m_isDaemon;
54: }
55:
56: /**
57: * Set the priority newly created <code>Thread</code>s should have
58: *
59: * @param priority One of {@link Thread#MIN_PRIORITY}, {@link
60: * Thread#NORM_PRIORITY}, {@link Thread#MAX_PRIORITY}
61: */
62: public void setPriority(final int priority) {
63: if ((Thread.MAX_PRIORITY == priority)
64: || (Thread.MIN_PRIORITY == priority)
65: || (Thread.NORM_PRIORITY == priority)) {
66: m_priority = priority;
67: }
68: }
69:
70: /**
71: * Get the priority newly created <code>Thread</code>s will have
72: *
73: * @return One of {@link Thread#MIN_PRIORITY}, {@link
74: * Thread#NORM_PRIORITY}, {@link Thread#MAX_PRIORITY}
75: */
76: public int getPriority() {
77: return m_priority;
78: }
79:
80: /**
81: * Create a new Thread for Runnable
82: *
83: * @param command The {@link Runnable}
84: *
85: * @return A new Thread instance
86: */
87: public Thread newThread(final Runnable command) {
88: final Thread thread = new Thread(command);
89: thread.setPriority(m_priority);
90: thread.setDaemon(m_isDaemon);
91:
92: return thread;
93: }
94: }
|