KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > WorkerThread


1 /**
2  * com.mckoi.database.WorkerThread 09 Sep 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import com.mckoi.debug.*;
28
29 /**
30  * This is a worker thread. This is given commands to execute by the
31  * WorkerPool.
32  *
33  * @author Tobias Downer
34  */

35
36 final class WorkerThread extends Thread JavaDoc {
37
38   /**
39    * If this is set to true, the server displays the time each executed
40    * command took.
41    */

42   private static final boolean DISPLAY_COMMAND_TIME = false;
43
44   /**
45    * Set to true to turn off this worker thread.
46    */

47   private boolean shutdown;
48
49   /**
50    * The Runnable command we are currently processing.
51    */

52   private Runnable JavaDoc command;
53
54   /**
55    * The time the command was started.
56    */

57   private long start_time;
58
59   /**
60    * The WorkerPool object that this worker thread is for.
61    */

62   private WorkerPool worker_pool;
63
64   /**
65    * Constructs the thread.
66    */

67   public WorkerThread(WorkerPool worker_pool) {
68     super();
69 // setDaemon(true);
70
setName("Mckoi - Worker");
71     this.worker_pool = worker_pool;
72     command = null;
73     shutdown = false;
74   }
75
76   /**
77    * Returns a DebugLogger object we can use to log debug messages.
78    */

79   public final DebugLogger Debug() {
80     return worker_pool.Debug();
81   }
82
83   // ---------- Other methods ----------
84

85   /**
86    * Shuts down this worker thread.
87    */

88   synchronized void shutdown() {
89     shutdown = true;
90     notifyAll();
91   }
92
93   /**
94    * Tells the worker thread that the user is executing the given command.
95    */

96   void execute(User user, DatabaseConnection database_connection,
97                Runnable JavaDoc runner) {
98     // This should help to prevent deadlock
99
synchronized (this) {
100       if (command == null) {
101         this.command = runner;
102         notifyAll();
103       }
104       else {
105         throw new RuntimeException JavaDoc(
106               "Deadlock Error, tried to execute command on running worker.");
107       }
108     }
109   }
110
111   /**
112    * Starts executing this worker thread.
113    */

114   public synchronized void run() {
115     while (true) {
116       try {
117         // Is there any command waiting to be executed?
118
if (command != null) {
119           try {
120             // Record the time this command was started.
121
start_time = System.currentTimeMillis();
122             // Run the command
123
command.run();
124           }
125           finally {
126             command = null;
127             // Record the time the command ended.
128
long elapsed_time = System.currentTimeMillis() - start_time;
129             if (DISPLAY_COMMAND_TIME) {
130               System.err.print("[Worker] Completed command in ");
131               System.err.print(elapsed_time);
132               System.err.print(" ms. ");
133               System.err.println(this);
134             }
135           }
136         }
137
138         // Notifies the thread pool manager that this worker is ready
139
// to go.
140
worker_pool.notifyWorkerReady(this);
141         // NOTE: The above command may cause a command to be posted on this
142
// worker.
143
while (command == null) {
144           try {
145             // Wait until there is a new command to process.
146
wait();
147           }
148           catch (InterruptedException JavaDoc e) { /* ignore */ }
149           // Shut down if we need to...
150
if (shutdown) {
151             return;
152           }
153         }
154
155       }
156       catch (Throwable JavaDoc e) {
157         Debug().write(Lvl.ERROR, this,
158             "Worker thread interrupted because of exception:\n" +
159             e.getMessage());
160         Debug().writeException(e);
161       }
162     }
163   }
164
165 }
166
Popular Tags