Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BatchingProgressMonitor.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. public Thread newThread(Runnable taskBody) {
  63. Thread thr = new Thread("JGit-AlarmQueue");
  64. thr.setDaemon(true);
  65. return thr;
  66. }
  67. });
  68. alarmQueue.allowCoreThreadTimeOut(false);
  69. alarmQueue.setMaximumPoolSize(alarmQueue.getCorePoolSize());
  70. alarmQueue.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
  71. alarmQueue.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
  72. alarmQueue.prestartAllCoreThreads();
  73. // Now that the threads are running, its critical to swap out
  74. // our own thread factory for one that isn't in the ClassLoader.
  75. // This allows the class to GC.
  76. //
  77. alarmQueue.setThreadFactory(Executors.defaultThreadFactory());
  78. alarmQueueKiller = new Object() {
  79. @Override
  80. protected void finalize() {
  81. alarmQueue.shutdownNow();
  82. }
  83. };
  84. }
  85. private long delayStartTime;
  86. private TimeUnit delayStartUnit = TimeUnit.MILLISECONDS;
  87. private Task task;
  88. /**
  89. * Set an optional delay before the first output.
  90. *
  91. * @param time
  92. * how long to wait before output. If 0 output begins on the
  93. * first {@link #update(int)} call.
  94. * @param unit
  95. * time unit of {@code time}.
  96. */
  97. public void setDelayStart(long time, TimeUnit unit) {
  98. delayStartTime = time;
  99. delayStartUnit = unit;
  100. }
  101. public void start(int totalTasks) {
  102. // Ignore the number of tasks.
  103. }
  104. public void beginTask(String title, int work) {
  105. endTask();
  106. task = new Task(title, work);
  107. if (delayStartTime != 0)
  108. task.delay(delayStartTime, delayStartUnit);
  109. }
  110. public void update(int completed) {
  111. if (task != null)
  112. task.update(this, completed);
  113. }
  114. public void endTask() {
  115. if (task != null) {
  116. task.end(this);
  117. task = null;
  118. }
  119. }
  120. public boolean isCancelled() {
  121. return false;
  122. }
  123. /**
  124. * Update the progress monitor if the total work isn't known,
  125. *
  126. * @param taskName
  127. * name of the task.
  128. * @param workCurr
  129. * number of units already completed.
  130. */
  131. protected abstract void onUpdate(String taskName, int workCurr);
  132. /**
  133. * Finish the progress monitor when the total wasn't known in advance.
  134. *
  135. * @param taskName
  136. * name of the task.
  137. * @param workCurr
  138. * total number of units processed.
  139. */
  140. protected abstract void onEndTask(String taskName, int workCurr);
  141. /**
  142. * Update the progress monitor when the total is known in advance.
  143. *
  144. * @param taskName
  145. * name of the task.
  146. * @param workCurr
  147. * number of units already completed.
  148. * @param workTotal
  149. * estimated number of units to process.
  150. * @param percentDone
  151. * {@code workCurr * 100 / workTotal}.
  152. */
  153. protected abstract void onUpdate(String taskName, int workCurr,
  154. int workTotal, int percentDone);
  155. /**
  156. * Finish the progress monitor when the total is known in advance.
  157. *
  158. * @param taskName
  159. * name of the task.
  160. * @param workCurr
  161. * total number of units processed.
  162. * @param workTotal
  163. * estimated number of units to process.
  164. * @param percentDone
  165. * {@code workCurr * 100 / workTotal}.
  166. */
  167. protected abstract void onEndTask(String taskName, int workCurr,
  168. int workTotal, int percentDone);
  169. private static class Task implements Runnable {
  170. /** Title of the current task. */
  171. private final String taskName;
  172. /** Number of work units, or {@link ProgressMonitor#UNKNOWN}. */
  173. private final int totalWork;
  174. /** True when timer expires and output should occur on next update. */
  175. private volatile boolean display;
  176. /** Scheduled timer, supporting cancellation if task ends early. */
  177. private Future<?> timerFuture;
  178. /** True if the task has displayed anything. */
  179. private boolean output;
  180. /** Number of work units already completed. */
  181. private int lastWork;
  182. /** Percentage of {@link #totalWork} that is done. */
  183. private int lastPercent;
  184. Task(String taskName, int totalWork) {
  185. this.taskName = taskName;
  186. this.totalWork = totalWork;
  187. this.display = true;
  188. }
  189. void delay(long time, TimeUnit unit) {
  190. display = false;
  191. timerFuture = alarmQueue.schedule(this, time, unit);
  192. }
  193. public void run() {
  194. display = true;
  195. }
  196. void update(BatchingProgressMonitor pm, int completed) {
  197. lastWork += completed;
  198. if (totalWork == UNKNOWN) {
  199. // Only display once per second, as the alarm fires.
  200. if (display) {
  201. pm.onUpdate(taskName, lastWork);
  202. output = true;
  203. restartTimer();
  204. }
  205. } else {
  206. // Display once per second or when 1% is done.
  207. int currPercent = lastWork * 100 / totalWork;
  208. if (display) {
  209. pm.onUpdate(taskName, lastWork, totalWork, currPercent);
  210. output = true;
  211. restartTimer();
  212. lastPercent = currPercent;
  213. } else if (currPercent != lastPercent) {
  214. pm.onUpdate(taskName, lastWork, totalWork, currPercent);
  215. output = true;
  216. lastPercent = currPercent;
  217. }
  218. }
  219. }
  220. private void restartTimer() {
  221. display = false;
  222. timerFuture = alarmQueue.schedule(this, 1, TimeUnit.SECONDS);
  223. }
  224. void end(BatchingProgressMonitor pm) {
  225. if (output) {
  226. if (totalWork == UNKNOWN) {
  227. pm.onEndTask(taskName, lastWork);
  228. } else {
  229. int pDone = lastWork * 100 / totalWork;
  230. pm.onEndTask(taskName, lastWork, totalWork, pDone);
  231. }
  232. }
  233. if (timerFuture != null)
  234. timerFuture.cancel(false /* no interrupt */);
  235. }
  236. }
  237. }