source: josm/trunk/src/org/openstreetmap/josm/gui/progress/swing/ProgressMonitorExecutor.java@ 14052

Last change on this file since 14052 was 14052, checked in by Don-vip, 6 years ago

see #16010 - use JMockit to enable more extensive test coverage (patch by ris, modified)

see https://github.com/openstreetmap/josm/pull/24/commits for details

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.progress.swing;
3
4import java.util.concurrent.CancellationException;
5import java.util.concurrent.ExecutionException;
6import java.util.concurrent.Future;
7import java.util.concurrent.LinkedBlockingQueue;
8import java.util.concurrent.ThreadPoolExecutor;
9import java.util.concurrent.TimeUnit;
10
11import org.openstreetmap.josm.tools.Logging;
12import org.openstreetmap.josm.tools.Utils;
13
14/**
15 * Executor that displays the progress monitor to the user.
16 *
17 * Similar to Executors.newSingleThreadExecutor(), but displays the
18 * progress monitor whenever a new task is executed.
19 * @since 12675 (moved from {@code gui.progress} package}
20 */
21public class ProgressMonitorExecutor extends ThreadPoolExecutor {
22
23 /**
24 * Creates a new {@code ProgressMonitorExecutor}
25 * @param nameFormat see {@link Utils#newThreadFactory(String, int)}
26 * @param threadPriority see {@link Utils#newThreadFactory(String, int)}
27 */
28 public ProgressMonitorExecutor(final String nameFormat, final int threadPriority) {
29 super(1, 1, 0L, TimeUnit.MILLISECONDS,
30 new LinkedBlockingQueue<Runnable>(),
31 Utils.newThreadFactory(nameFormat, threadPriority));
32 }
33
34 @Override
35 public void execute(Runnable command) {
36 if (PleaseWaitProgressMonitor.currentProgressMonitor != null) {
37 //TODO show only if this can't be in background or better if always in background is not checked
38 PleaseWaitProgressMonitor.currentProgressMonitor.showForegroundDialog();
39 }
40 super.execute(command);
41 }
42
43 @Override
44 public void afterExecute(final Runnable r, Throwable t) {
45 // largely as proposed by JDK8 docs
46 super.afterExecute(r, t);
47 if (t == null && r instanceof Future<?>) {
48 try {
49 ((Future<?>) r).get();
50 } catch (CancellationException cancellationException) {
51 t = cancellationException;
52 } catch (ExecutionException executionException) {
53 t = executionException.getCause();
54 } catch (InterruptedException interruptedException) {
55 Thread.currentThread().interrupt(); // ignore/reset
56 }
57 }
58 if (t != null) {
59 Logging.error("Thread {0} raised {1}", Thread.currentThread().getName(), t);
60 }
61 }
62}
Note: See TracBrowser for help on using the repository browser.