PageRenderTime 207ms CodeModel.GetById 61ms app.highlight 20ms RepoModel.GetById 20ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/util/WorkThread.java

#
Java | 225 lines | 144 code | 22 blank | 59 comment | 7 complexity | 9e25a581080a41f301fe00c8e7725ffa MD5 | raw file
  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 3905 2001-11-23 09:08:49Z spestov $
 26 */
 27public class WorkThread extends Thread
 28{
 29	public WorkThread(WorkThreadPool pool, ThreadGroup group, String name)
 30	{
 31		super(group, name);
 32		setDaemon(true);
 33		setPriority(4);
 34
 35		this.pool = pool;
 36	}
 37
 38	/**
 39	 * Sets if the current request can be aborted.
 40	 * @since jEdit 2.6pre1
 41	 */
 42	public void setAbortable(boolean abortable)
 43	{
 44		synchronized(abortLock)
 45		{
 46			this.abortable = abortable;
 47			if(aborted)
 48				stop(new Abort());
 49		}
 50	}
 51
 52	/**
 53	 * Returns if the work thread is currently running a request.
 54	 */
 55	public boolean isRequestRunning()
 56	{
 57		return requestRunning;
 58	}
 59
 60	/**
 61	 * Returns the status text.
 62	 */
 63	public String getStatus()
 64	{
 65		return status;
 66	}
 67
 68	/**
 69	 * Sets the status text.
 70	 * @since jEdit 2.6pre1
 71	 */
 72	public void setStatus(String status)
 73	{
 74		this.status = status;
 75		pool.fireProgressChanged(this);
 76	}
 77
 78	/**
 79	 * Returns the progress value.
 80	 */
 81	public int getProgressValue()
 82	{
 83		return progressValue;
 84	}
 85
 86	/**
 87	 * Sets the progress value.
 88	 * @since jEdit 2.6pre1
 89	 */
 90	public void setProgressValue(int progressValue)
 91	{
 92		this.progressValue = progressValue;
 93		pool.fireProgressChanged(this);
 94	}
 95
 96	/**
 97	 * Returns the progress maximum.
 98	 */
 99	public int getProgressMaximum()
100	{
101		return progressMaximum;
102	}
103
104	/**
105	 * Sets the maximum progress value.
106	 * @since jEdit 2.6pre1
107	 */
108	public void setProgressMaximum(int progressMaximum)
109	{
110		this.progressMaximum = progressMaximum;
111		pool.fireProgressChanged(this);
112	}
113
114	/**
115	 * Aborts the currently running request, if allowed.
116	 * @since jEdit 2.6pre1
117	 */
118	public void abortCurrentRequest()
119	{
120		synchronized(abortLock)
121		{
122			if(abortable && !aborted)
123				stop(new Abort());
124			aborted = true;
125		}
126	}
127
128	public void run()
129	{
130		Log.log(Log.DEBUG,this,"Work request thread starting [" + getName() + "]");
131
132		for(;;)
133		{
134			doRequests();
135		}
136	}
137
138	// private members
139	private WorkThreadPool pool;
140	private Object abortLock = new Object();
141	private boolean requestRunning;
142	private boolean abortable;
143	private boolean aborted;
144	private String status;
145	private int progressValue;
146	private int progressMaximum;
147
148	private void doRequests()
149	{
150		WorkThreadPool.Request request;
151		for(;;)
152		{
153			request = pool.getNextRequest();
154			if(request == null)
155				break;
156			else
157			{
158				requestRunning = true;
159				pool.fireStatusChanged(this);
160				doRequest(request);
161				requestRunning = false;
162			}
163		}
164
165		pool.fireStatusChanged(this);
166
167		synchronized(pool.waitForAllLock)
168		{
169			// notify a running waitForRequests() method
170			pool.waitForAllLock.notifyAll();
171		}
172
173		synchronized(pool.lock)
174		{
175			// wait for more requests
176			try
177			{
178				pool.lock.wait();
179			}
180			catch(InterruptedException ie)
181			{
182				Log.log(Log.ERROR,this,ie);
183			}
184		}
185	}
186
187	private void doRequest(WorkThreadPool.Request request)
188	{
189//		Log.log(Log.DEBUG,WorkThread.class,"Running in work thread: " + request);
190
191		try
192		{
193			request.run.run();
194		}
195		catch(Abort a)
196		{
197			Log.log(Log.ERROR,WorkThread.class,"Unhandled abort");
198		}
199		catch(Throwable t)
200		{
201			Log.log(Log.ERROR,WorkThread.class,"Exception "
202				+ "in work thread:");
203			Log.log(Log.ERROR,WorkThread.class,t);
204		}
205		finally
206		{
207			synchronized(abortLock)
208			{
209				aborted = abortable = false;
210			}
211			status = null;
212			progressValue = progressMaximum = 0;
213			pool.requestDone();
214			pool.fireStatusChanged(this);
215		}
216	}
217
218	public static class Abort extends Error
219	{
220		public Abort()
221		{
222			super("Work request aborted");
223		}
224	}
225}