KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > connector > grizzly > GrizzlyThreadFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.web.connector.grizzly;
25
26 import java.util.concurrent.ThreadFactory JavaDoc;
27
28 /**
29  * Customized <code>ThreadFactory</code> used by the <code>Pipeline</code>
30  * instance.
31  *
32  * @author Jean-Francois Arcand
33  */

34 public class GrizzlyThreadFactory implements ThreadFactory JavaDoc{
35
36     /**
37      * The name used when creating threads
38      */

39     protected String JavaDoc name;
40     
41     /**
42      * The port used when created threads' name.
43      */

44     protected int port;
45     
46     /**
47      * The number of created threads.
48      */

49     protected int threadCount;
50     
51     
52     /**
53      * The priority used when creating threads.
54      */

55     protected int priority;
56
57     
58     /**
59      * The <code>ThreadGroup</code> used.
60      */

61     private final static ThreadGroup JavaDoc threadGroup = new ThreadGroup JavaDoc("Grizzly");
62     
63     /**
64      * Create an instance of <code>ThreadFactory</code>
65      * @param name the name of thread who will be created by this factory
66      * @param port the port of thread who will be created by this factory
67      * @param priority the priority of thread who will be created by this factory
68      */

69     public GrizzlyThreadFactory(String JavaDoc name, int port,int priority){
70         this.name = name;
71         this.port = port;
72         this.priority = priority;
73     }
74
75
76     /**
77      * Create a new thread.
78      * @param r an instance of a <code>Task</code>.
79      * @return a new Thread.
80      */

81     public Thread JavaDoc newThread(Runnable JavaDoc r){
82         Thread JavaDoc t = new Thread JavaDoc(threadGroup,r);
83         t.setName(name + "WorkerThread-" + port + "-" + threadCount);
84         t.setPriority(priority);
85         t.setDaemon(true);
86       
87         threadCount++;
88         return t;
89     }
90
91     
92     /**
93      * Return the <code>ThreadGroup</code> used by this factory
94      */

95     public ThreadGroup JavaDoc getThreadGroup(){
96         return threadGroup;
97     }
98     
99     
100     /**
101      * Interrupt the <code>Thread</code> using it thread id
102      */

103     public boolean interruptThread(long threadID){
104         Thread JavaDoc[] threads = new Thread JavaDoc[threadGroup.activeCount()];
105         threadGroup.enumerate(threads);
106                
107         for (Thread JavaDoc thread: threads){
108             if ( thread != null && thread.getId() == threadID ){
109                 if ( Thread.State.RUNNABLE != thread.getState()){
110                     try{
111                         thread.interrupt();
112                         return true;
113                     } catch (Throwable JavaDoc t){
114                         ; // Swallow any exceptions.
115
}
116                 }
117             }
118         }
119         return false;
120     }
121 }
122
Popular Tags