KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > concurrent > NamedThreadFactory


1 /*
2  * $Id: NamedThreadFactory.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.util.concurrent;
12
13 import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
14 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong;
15
16 import org.apache.commons.lang.StringUtils;
17
18 public class NamedThreadFactory implements ThreadFactory
19 {
20     private final String JavaDoc name;
21     private final int priority;
22     private final AtomicLong counter;
23
24     public NamedThreadFactory(String JavaDoc name)
25     {
26         this(name, Thread.NORM_PRIORITY);
27     }
28
29     public NamedThreadFactory(String JavaDoc name, int priority)
30     {
31         if (StringUtils.isEmpty(name))
32         {
33             throw new IllegalArgumentException JavaDoc("NamedThreadFactory must have a proper name.");
34         }
35
36         this.name = name;
37         this.priority = priority;
38         this.counter = new AtomicLong(1);
39     }
40
41     public Thread JavaDoc newThread(Runnable JavaDoc runnable)
42     {
43         Thread JavaDoc t = new Thread JavaDoc(runnable, name + '.' + counter.getAndIncrement());
44         t.setPriority(priority);
45         return t;
46     }
47
48 }
49
Popular Tags