You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BatchingProgressMonitor.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (C) 2008-2011, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.lib;
  44. import java.util.concurrent.Executors;
  45. import java.util.concurrent.Future;
  46. import java.util.concurrent.ScheduledThreadPoolExecutor;
  47. import java.util.concurrent.ThreadFactory;
  48. import java.util.concurrent.TimeUnit;
  49. /** ProgressMonitor that batches update events. */
  50. public abstract class BatchingProgressMonitor implements ProgressMonitor {
  51. private static final ScheduledThreadPoolExecutor alarmQueue;
  52. static final Object alarmQueueKiller;
  53. static {
  54. // To support garbage collection, start our thread but
  55. // swap out the thread factory. When our class is GC'd
  56. // the alarmQueueKiller will finalize and ask the executor
  57. // to shutdown, ending the worker.
  58. //
  59. int threads = 1;
  60. alarmQueue = new ScheduledThreadPoolExecutor(threads,
  61. new ThreadFactory() {
  62. private final ThreadFactory baseFactory = Executors
  63. .defaultThreadFactory();
  64. public Thread newThread(Runnable taskBody) {
  65. Thread thr = baseFactory.newThread(taskBody);
  66. thr.setName("JGit-AlarmQueue");
  67. thr.setDaemon(true);
  68. return thr;
  69. }
  70. });
  71. alarmQueue.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
  72. alarmQueue.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
  73. alarmQueue.prestartAllCoreThreads();
  74. // Now that the threads are running, its critical to swap out
  75. // our own thread factory for one that isn't in the ClassLoader.
  76. // This allows the class to GC.
  77. //
  78. alarmQueue.setThreadFactory(Executors.defaultThreadFactory());
  79. alarmQueueKiller = new Object() {
  80. @Override
  81. protected void finalize() {
  82. alarmQueue.shutdownNow();
  83. }
  84. };
  85. }
  86. private long delayStartTime;
  87. private TimeUnit delayStartUnit = TimeUnit.MILLISECONDS;
  88. private Task task;
  89. /**
  90. * Set an optional delay before the first output.
  91. *
  92. * @param time
  93. * how long to wait before output. If 0 output begins on the
  94. * first {@link #update(int)} call.
  95. * @param unit
  96. * time unit of {@code time}.
  97. */
  98. public void setDelayStart(long time, TimeUnit unit) {
  99. delayStartTime = time;
  100. delayStartUnit = unit;
  101. }
  102. public void start(int totalTasks) {
  103. // Ignore the number of tasks.
  104. }
  105. public void beginTask(String title, int work) {
  106. endTask();
  107. task = new Task(title, work);
  108. if (delayStartTime != 0)
  109. task.delay(delayStartTime, delayStartUnit);
  110. }
  111. public void update(int completed) {
  112. if (task != null)
  113. task.update(this, completed);
  114. }
  115. public void endTask() {
  116. if (task != null) {
  117. task.end(this);
  118. task = null;
  119. }
  120. }
  121. public boolean isCancelled() {
  122. return false;
  123. }
  124. /**
  125. * Update the progress monitor if the total work isn't known,
  126. *
  127. * @param taskName
  128. * name of the task.
  129. * @param workCurr
  130. * number of units already completed.
  131. */
  132. protected abstract void onUpdate(String taskName, int workCurr);
  133. /**
  134. * Finish the progress monitor when the total wasn't known in advance.
  135. *
  136. * @param taskName
  137. * name of the task.
  138. * @param workCurr
  139. * total number of units processed.
  140. */
  141. protected abstract void onEndTask(String taskName, int workCurr);
  142. /**
  143. * Update the progress monitor when the total is known in advance.
  144. *
  145. * @param taskName
  146. * name of the task.
  147. * @param workCurr
  148. * number of units already completed.
  149. * @param workTotal
  150. * estimated number of units to process.
  151. * @param percentDone
  152. * {@code workCurr * 100 / workTotal}.
  153. */
  154. protected abstract void onUpdate(String taskName, int workCurr,
  155. int workTotal, int percentDone);
  156. /**
  157. * Finish the progress monitor when the total is known in advance.
  158. *
  159. * @param taskName
  160. * name of the task.
  161. * @param workCurr
  162. * total number of units processed.
  163. * @param workTotal
  164. * estimated number of units to process.
  165. * @param percentDone
  166. * {@code workCurr * 100 / workTotal}.
  167. */
  168. protected abstract void onEndTask(String taskName, int workCurr,
  169. int workTotal, int percentDone);
  170. private static class Task implements Runnable {
  171. /** Title of the current task. */
  172. private final String taskName;
  173. /** Number of work units, or {@link ProgressMonitor#UNKNOWN}. */
  174. private final int totalWork;
  175. /** True when timer expires and output should occur on next update. */
  176. private volatile boolean display;
  177. /** Scheduled timer, supporting cancellation if task ends early. */
  178. private Future<?> timerFuture;
  179. /** True if the task has displayed anything. */
  180. private boolean output;
  181. /** Number of work units already completed. */
  182. private int lastWork;
  183. /** Percentage of {@link #totalWork} that is done. */
  184. private int lastPercent;
  185. Task(String taskName, int totalWork) {
  186. this.taskName = taskName;
  187. this.totalWork = totalWork;
  188. this.display = true;
  189. }
  190. void delay(long time, TimeUnit unit) {
  191. display = false;
  192. timerFuture = alarmQueue.schedule(this, time, unit);
  193. }
  194. public void run() {
  195. display = true;
  196. }
  197. void update(BatchingProgressMonitor pm, int completed) {
  198. lastWork += completed;
  199. if (totalWork == UNKNOWN) {
  200. // Only display once per second, as the alarm fires.
  201. if (display) {
  202. pm.onUpdate(taskName, lastWork);
  203. output = true;
  204. restartTimer();
  205. }
  206. } else {
  207. // Display once per second or when 1% is done.
  208. int currPercent = lastWork * 100 / totalWork;
  209. if (display) {
  210. pm.onUpdate(taskName, lastWork, totalWork, currPercent);
  211. output = true;
  212. restartTimer();
  213. lastPercent = currPercent;
  214. } else if (currPercent != lastPercent) {
  215. pm.onUpdate(taskName, lastWork, totalWork, currPercent);
  216. output = true;
  217. lastPercent = currPercent;
  218. }
  219. }
  220. }
  221. private void restartTimer() {
  222. display = false;
  223. timerFuture = alarmQueue.schedule(this, 1, TimeUnit.SECONDS);
  224. }
  225. void end(BatchingProgressMonitor pm) {
  226. if (output) {
  227. if (totalWork == UNKNOWN) {
  228. pm.onEndTask(taskName, lastWork);
  229. } else {
  230. int pDone = lastWork * 100 / totalWork;
  231. pm.onEndTask(taskName, lastWork, totalWork, pDone);
  232. }
  233. }
  234. if (timerFuture != null)
  235. timerFuture.cancel(false /* no interrupt */);
  236. }
  237. }
  238. }