/jEdit/tags/jedit-4-3-2/org/gjt/sp/util/WorkThread.java

# · Java · 243 lines · 149 code · 24 blank · 70 comment · 7 complexity · 6bba13f8ee22d471ff6d085145a01dbe 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. package org.gjt.sp.util;
  20. /**
  21. * Services work requests in the background.
  22. * @author Slava Pestov
  23. * @version $Id: WorkThread.java 12504 2008-04-22 23:12:43Z ezust $
  24. */
  25. public class WorkThread extends Thread implements ThreadAbortMonitor
  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. * If set to true and already aborted, the thread will be stopped
  38. *
  39. * @param abortable true if the WorkThread is abortable
  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. * Returns if the work thread is currently running a request.
  53. * @return true if a request is currently running
  54. */
  55. public boolean isRequestRunning()
  56. {
  57. return requestRunning;
  58. }
  59. public boolean isAborted()
  60. {
  61. synchronized (abortLock)
  62. {
  63. return aborted;
  64. }
  65. }
  66. /**
  67. * Returns the status text.
  68. * @return the status label
  69. */
  70. public String getStatus()
  71. {
  72. return status;
  73. }
  74. /**
  75. * Sets the status text.
  76. * @param status the new status of the thread
  77. * @since jEdit 2.6pre1
  78. */
  79. public void setStatus(String status)
  80. {
  81. this.status = status;
  82. pool.fireProgressChanged(this);
  83. }
  84. /**
  85. * Returns the progress value.
  86. * @return the progress value
  87. */
  88. public int getProgressValue()
  89. {
  90. return progressValue;
  91. }
  92. /**
  93. * Sets the progress value.
  94. * @param progressValue the new progress value
  95. * @since jEdit 2.6pre1
  96. */
  97. public void setProgressValue(int progressValue)
  98. {
  99. this.progressValue = progressValue;
  100. pool.fireProgressChanged(this);
  101. }
  102. /**
  103. * Returns the progress maximum.
  104. * @return the maximum value of the progression
  105. */
  106. public int getProgressMaximum()
  107. {
  108. return progressMaximum;
  109. }
  110. /**
  111. * Sets the maximum progress value.
  112. * @param progressMaximum the maximum value of the progression
  113. * @since jEdit 2.6pre1
  114. */
  115. public void setProgressMaximum(int progressMaximum)
  116. {
  117. this.progressMaximum = progressMaximum;
  118. pool.fireProgressChanged(this);
  119. }
  120. /**
  121. * Aborts the currently running request, if allowed.
  122. * @since jEdit 2.6pre1
  123. */
  124. public void abortCurrentRequest()
  125. {
  126. synchronized(abortLock)
  127. {
  128. if(abortable && !aborted)
  129. stop(new Abort());
  130. aborted = true;
  131. }
  132. }
  133. public void run()
  134. {
  135. Log.log(Log.DEBUG,this,"Work request thread starting [" + getName() + "]");
  136. for(;;)
  137. {
  138. doRequests();
  139. }
  140. }
  141. // private members
  142. private WorkThreadPool pool;
  143. private final Object abortLock = new Object();
  144. private boolean requestRunning;
  145. private boolean abortable;
  146. private boolean aborted;
  147. private String status;
  148. private int progressValue;
  149. private int progressMaximum;
  150. private void doRequests()
  151. {
  152. WorkThreadPool.Request request;
  153. for(;;)
  154. {
  155. request = pool.getNextRequest();
  156. if(request == null)
  157. break;
  158. else
  159. {
  160. requestRunning = true;
  161. pool.fireStatusChanged(this);
  162. doRequest(request);
  163. requestRunning = false;
  164. }
  165. }
  166. pool.fireStatusChanged(this);
  167. synchronized(pool.waitForAllLock)
  168. {
  169. // notify a running waitForRequests() method
  170. pool.waitForAllLock.notifyAll();
  171. }
  172. synchronized(pool.lock)
  173. {
  174. // wait for more requests
  175. try
  176. {
  177. pool.lock.wait();
  178. }
  179. catch(InterruptedException ie)
  180. {
  181. Log.log(Log.ERROR,this,ie);
  182. }
  183. }
  184. }
  185. private void doRequest(WorkThreadPool.Request request)
  186. {
  187. Log.log(Log.DEBUG,WorkThread.class,"Running in work thread: " + request);
  188. try
  189. {
  190. request.run.run();
  191. }
  192. catch(Abort a)
  193. {
  194. Log.log(Log.ERROR,WorkThread.class,"Unhandled abort", a);
  195. }
  196. catch(Throwable t)
  197. {
  198. Log.log(Log.ERROR,WorkThread.class,"Exception in work thread: ", t);
  199. }
  200. finally
  201. {
  202. synchronized(abortLock)
  203. {
  204. aborted = abortable = false;
  205. }
  206. status = null;
  207. progressValue = progressMaximum = 0;
  208. pool.requestDone();
  209. pool.fireStatusChanged(this);
  210. }
  211. }
  212. public static class Abort extends Error
  213. {
  214. public Abort()
  215. {
  216. super("Work request aborted");
  217. }
  218. }
  219. }