PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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