KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > util > WorkThread


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
20 package org.gjt.sp.util;
21
22 /**
23  * Services work requests in the background.
24  * @author Slava Pestov
25  * @version $Id: WorkThread.java 8175 2006-12-05 20:36:33Z kpouer $
26  */

27 public class WorkThread extends Thread JavaDoc implements ThreadAbortMonitor
28 {
29     public WorkThread(WorkThreadPool pool, ThreadGroup JavaDoc group, String JavaDoc 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     public boolean isAborted()
63     {
64         synchronized(abortLock)
65         {
66             return aborted;
67         }
68     }
69
70     /**
71      * Returns the status text.
72      */

73     public String JavaDoc getStatus()
74     {
75         return status;
76     }
77
78     /**
79      * Sets the status text.
80      * @since jEdit 2.6pre1
81      */

82     public void setStatus(String JavaDoc status)
83     {
84         this.status = status;
85         pool.fireProgressChanged(this);
86     }
87
88     /**
89      * Returns the progress value.
90      */

91     public int getProgressValue()
92     {
93         return progressValue;
94     }
95
96     /**
97      * Sets the progress value.
98      * @since jEdit 2.6pre1
99      */

100     public void setProgressValue(int progressValue)
101     {
102         this.progressValue = progressValue;
103         pool.fireProgressChanged(this);
104     }
105
106     /**
107      * Returns the progress maximum.
108      */

109     public int getProgressMaximum()
110     {
111         return progressMaximum;
112     }
113
114     /**
115      * Sets the maximum progress value.
116      * @since jEdit 2.6pre1
117      */

118     public void setProgressMaximum(int progressMaximum)
119     {
120         this.progressMaximum = progressMaximum;
121         pool.fireProgressChanged(this);
122     }
123
124     /**
125      * Aborts the currently running request, if allowed.
126      * @since jEdit 2.6pre1
127      */

128     public void abortCurrentRequest()
129     {
130         synchronized(abortLock)
131         {
132             if(abortable && !aborted)
133                 stop(new Abort());
134             aborted = true;
135         }
136     }
137
138     public void run()
139     {
140         Log.log(Log.DEBUG,this,"Work request thread starting [" + getName() + "]");
141
142         for(;;)
143         {
144             doRequests();
145         }
146     }
147
148     // private members
149
private WorkThreadPool pool;
150     private Object JavaDoc abortLock = new Object JavaDoc();
151     private boolean requestRunning;
152     private boolean abortable;
153     private boolean aborted;
154     private String JavaDoc status;
155     private int progressValue;
156     private int progressMaximum;
157
158     private void doRequests()
159     {
160         WorkThreadPool.Request request;
161         for(;;)
162         {
163             request = pool.getNextRequest();
164             if(request == null)
165                 break;
166             else
167             {
168                 requestRunning = true;
169                 pool.fireStatusChanged(this);
170                 doRequest(request);
171                 requestRunning = false;
172             }
173         }
174
175         pool.fireStatusChanged(this);
176
177         synchronized(pool.waitForAllLock)
178         {
179             // notify a running waitForRequests() method
180
pool.waitForAllLock.notifyAll();
181         }
182
183         synchronized(pool.lock)
184         {
185             // wait for more requests
186
try
187             {
188                 pool.lock.wait();
189             }
190             catch(InterruptedException JavaDoc ie)
191             {
192                 Log.log(Log.ERROR,this,ie);
193             }
194         }
195     }
196
197     private void doRequest(WorkThreadPool.Request request)
198     {
199         Log.log(Log.DEBUG,WorkThread.class,"Running in work thread: " + request);
200
201         try
202         {
203             request.run.run();
204         }
205         catch(Abort a)
206         {
207             Log.log(Log.ERROR,WorkThread.class,"Unhandled abort", a);
208         }
209         catch(Throwable JavaDoc t)
210         {
211             Log.log(Log.ERROR,WorkThread.class,"Exception in work thread: ", t);
212         }
213         finally
214         {
215             synchronized(abortLock)
216             {
217                 aborted = abortable = false;
218             }
219             status = null;
220             progressValue = progressMaximum = 0;
221             pool.requestDone();
222             pool.fireStatusChanged(this);
223         }
224     }
225
226     public static class Abort extends Error JavaDoc
227     {
228         public Abort()
229         {
230             super("Work request aborted");
231         }
232     }
233 }
234
Popular Tags