/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/util/WorkThread.java
Java | 226 lines | 144 code | 22 blank | 60 comment | 7 complexity | 12a931045a380f4a550597f06a237fde MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
1/*
2 * WorkThread.java - Background thread that does stuff
3 * Copyright (C) 2000 Slava Pestov
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20package org.gjt.sp.util;
21
22/**
23 * Services work requests in the background.
24 * @author Slava Pestov
25 * @version $Id: WorkThread.java 4543 2003-03-12 17:01:50Z spestov $
26 */
27public class WorkThread extends Thread
28{
29 public WorkThread(WorkThreadPool pool, ThreadGroup group, String name)
30 {
31 super(group, name);
32 // so that jEdit doesn't exit with no views open automatically
33 //setDaemon(true);
34 setPriority(Thread.MIN_PRIORITY);
35
36 this.pool = pool;
37 }
38
39 /**
40 * Sets if the current request can be aborted.
41 * @since jEdit 2.6pre1
42 */
43 public void setAbortable(boolean abortable)
44 {
45 synchronized(abortLock)
46 {
47 this.abortable = abortable;
48 if(aborted)
49 stop(new Abort());
50 }
51 }
52
53 /**
54 * Returns if the work thread is currently running a request.
55 */
56 public boolean isRequestRunning()
57 {
58 return requestRunning;
59 }
60
61 /**
62 * Returns the status text.
63 */
64 public String getStatus()
65 {
66 return status;
67 }
68
69 /**
70 * Sets the status text.
71 * @since jEdit 2.6pre1
72 */
73 public void setStatus(String status)
74 {
75 this.status = status;
76 pool.fireProgressChanged(this);
77 }
78
79 /**
80 * Returns the progress value.
81 */
82 public int getProgressValue()
83 {
84 return progressValue;
85 }
86
87 /**
88 * Sets the progress value.
89 * @since jEdit 2.6pre1
90 */
91 public void setProgressValue(int progressValue)
92 {
93 this.progressValue = progressValue;
94 pool.fireProgressChanged(this);
95 }
96
97 /**
98 * Returns the progress maximum.
99 */
100 public int getProgressMaximum()
101 {
102 return progressMaximum;
103 }
104
105 /**
106 * Sets the maximum progress value.
107 * @since jEdit 2.6pre1
108 */
109 public void setProgressMaximum(int progressMaximum)
110 {
111 this.progressMaximum = progressMaximum;
112 pool.fireProgressChanged(this);
113 }
114
115 /**
116 * Aborts the currently running request, if allowed.
117 * @since jEdit 2.6pre1
118 */
119 public void abortCurrentRequest()
120 {
121 synchronized(abortLock)
122 {
123 if(abortable && !aborted)
124 stop(new Abort());
125 aborted = true;
126 }
127 }
128
129 public void run()
130 {
131 Log.log(Log.DEBUG,this,"Work request thread starting [" + getName() + "]");
132
133 for(;;)
134 {
135 doRequests();
136 }
137 }
138
139 // private members
140 private WorkThreadPool pool;
141 private Object abortLock = new Object();
142 private boolean requestRunning;
143 private boolean abortable;
144 private boolean aborted;
145 private String status;
146 private int progressValue;
147 private int progressMaximum;
148
149 private void doRequests()
150 {
151 WorkThreadPool.Request request;
152 for(;;)
153 {
154 request = pool.getNextRequest();
155 if(request == null)
156 break;
157 else
158 {
159 requestRunning = true;
160 pool.fireStatusChanged(this);
161 doRequest(request);
162 requestRunning = false;
163 }
164 }
165
166 pool.fireStatusChanged(this);
167
168 synchronized(pool.waitForAllLock)
169 {
170 // notify a running waitForRequests() method
171 pool.waitForAllLock.notifyAll();
172 }
173
174 synchronized(pool.lock)
175 {
176 // wait for more requests
177 try
178 {
179 pool.lock.wait();
180 }
181 catch(InterruptedException ie)
182 {
183 Log.log(Log.ERROR,this,ie);
184 }
185 }
186 }
187
188 private void doRequest(WorkThreadPool.Request request)
189 {
190 Log.log(Log.DEBUG,WorkThread.class,"Running in work thread: " + request);
191
192 try
193 {
194 request.run.run();
195 }
196 catch(Abort a)
197 {
198 Log.log(Log.ERROR,WorkThread.class,"Unhandled abort");
199 }
200 catch(Throwable t)
201 {
202 Log.log(Log.ERROR,WorkThread.class,"Exception "
203 + "in work thread:");
204 Log.log(Log.ERROR,WorkThread.class,t);
205 }
206 finally
207 {
208 synchronized(abortLock)
209 {
210 aborted = abortable = false;
211 }
212 status = null;
213 progressValue = progressMaximum = 0;
214 pool.requestDone();
215 pool.fireStatusChanged(this);
216 }
217 }
218
219 public static class Abort extends Error
220 {
221 public Abort()
222 {
223 super("Work request aborted");
224 }
225 }
226}