01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.synapse.util.concurrent;
21:
22: import java.util.concurrent.atomic.AtomicInteger;
23: import java.util.concurrent.ThreadFactory;
24:
25: /**
26: * This is the thread factory for Synapse threads which are accessible through the
27: * SynapseEnvironment as pooled threads.
28: */
29: public class SynapseThreadFactory implements ThreadFactory {
30:
31: /** Holds the ThreadGroup under which this factory creates threads */
32: final ThreadGroup group;
33:
34: /** Holds the AtomicInteger class instance for the factory */
35: final AtomicInteger count;
36:
37: /** prefix for the thread id, thread number will be followed to construct the id */
38: final String namePrefix;
39:
40: /**
41: * Constructor for the ThreadFactory to create new threads
42: *
43: * @param group - all the threads are created under this group by this factory
44: * @param namePrefix - name prefix of the threads created by this factory
45: */
46: public SynapseThreadFactory(final ThreadGroup group,
47: final String namePrefix) {
48: super ();
49: this .count = new AtomicInteger(1);
50: this .group = group;
51: this .namePrefix = namePrefix;
52: }
53:
54: /**
55: * This method is the implementation of the the newThread method and will
56: * create new threads under the group and with the nameprefix followed by the
57: * thread number as the id
58: *
59: * @param runnable - Runnable class to run by the created thread
60: * @return a Thread executing the given runnable
61: */
62: public Thread newThread(final Runnable runnable) {
63: StringBuffer buffer = new StringBuffer();
64: buffer.append(this .namePrefix);
65: buffer.append('-');
66: buffer.append(this .count.getAndIncrement());
67: Thread t = new Thread(group, runnable, buffer.toString(), 0);
68: t.setDaemon(false);
69: t.setPriority(Thread.NORM_PRIORITY);
70: return t;
71: }
72:
73: }
|