PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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