KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.web.connector.grizzly;
24
25 import java.util.logging.Level JavaDoc;
26
27
28
29 /**
30  * Simple worker thread used for processing HTTP requests. All threads are
31  * synchronized using a <code>Pipeline</code> object
32  *
33  * @author Jean-Francois Arcand
34  */

35 public class WorkerThread extends Thread JavaDoc{
36
37     /**
38      * The <code>Pipeline</code> on which this thread synchronize.
39      */

40     private Pipeline pipeline;
41
42     /**
43      * Looing variable.
44      */

45     private volatile boolean doTask = true;
46
47     
48     /**
49      * The <code>ThreadGroup</code> used.
50      */

51     private final static ThreadGroup JavaDoc threadGroup = new ThreadGroup JavaDoc("Grizzly");
52     
53     
54     /**
55      * Create a Thread that will synchronizes/block on throws
56      * <code>Pipeline</code> instance.
57      */

58     public WorkerThread(Pipeline pipeline, String JavaDoc name){
59         super(threadGroup, name);
60         this.pipeline = pipeline;
61         setDaemon(true);
62     }
63
64     
65     /**
66      * Perform a <code>Task</code>.processing
67      */

68     public void run(){
69
70         while (doTask) {
71             try{
72                 // Wait for a Task to be added to the pipeline.
73
Task t = pipeline.getTask();
74                 if ( t != null){
75                     t.run();
76                     t = null;
77                 }
78             } catch (Throwable JavaDoc t) {
79                 SelectorThread.logger().log(Level.FINE,
80                         "workerThread.httpException",t);
81             }
82         }
83     }
84     
85     
86     /**
87      * Stop this thread. If this Thread is performing atask, the task will be
88      * completed.
89      */

90     public void terminate(){
91         doTask = false;
92     }
93 }
94
95
Popular Tags