|
13 | 13 | import java.util.concurrent.CompletionException; |
14 | 14 | import java.util.concurrent.ExecutorService; |
15 | 15 | import java.util.concurrent.Future; |
| 16 | +import java.util.concurrent.Semaphore; |
16 | 17 | import java.util.concurrent.TimeUnit; |
17 | | -import java.util.concurrent.locks.ReentrantLock; |
18 | 18 |
|
19 | 19 | @Log4j2 |
20 | 20 | public class ConversionProgressExecutor implements ExecutorService, AutoCloseable{ |
21 | 21 | private final ExecutorService delegate; |
22 | 22 | private final ProgressBar progressBar; |
23 | | - private final ReentrantLock progressBarLock; |
| 23 | + private final Semaphore progressBarLock; |
24 | 24 |
|
25 | 25 | public ConversionProgressExecutor(@NonNull ExecutorService executorService){ |
26 | 26 | delegate = executorService; |
27 | 27 | progressBar = new ProgressBarBuilder() |
28 | 28 | .setTaskName("Conversion") |
29 | 29 | .setInitialMax(-1) |
30 | 30 | .build(); |
31 | | - progressBarLock = new ReentrantLock(); |
| 31 | + progressBarLock = new Semaphore(1); |
32 | 32 | } |
33 | 33 |
|
34 | 34 | public static ConversionProgressExecutor of(@NonNull ExecutorService executorService){ |
@@ -88,22 +88,28 @@ public <T> Future<T> submit(@NonNull Callable<T> task){ |
88 | 88 | } |
89 | 89 |
|
90 | 90 | private void incrementProgressbarMax(){ |
91 | | - progressBarLock.lock(); |
92 | 91 | try{ |
| 92 | + progressBarLock.acquire(); |
93 | 93 | progressBar.maxHint(progressBar.getMax() + 1); |
94 | 94 | } |
| 95 | + catch(InterruptedException e){ |
| 96 | + throw new RuntimeException(e); |
| 97 | + } |
95 | 98 | finally{ |
96 | | - progressBarLock.unlock(); |
| 99 | + progressBarLock.release(); |
97 | 100 | } |
98 | 101 | } |
99 | 102 |
|
100 | 103 | private void stepProgressBar(long amount){ |
101 | | - progressBarLock.lock(); |
102 | 104 | try{ |
| 105 | + progressBarLock.acquire(); |
103 | 106 | progressBar.stepBy(amount); |
104 | 107 | } |
| 108 | + catch(InterruptedException e){ |
| 109 | + throw new RuntimeException(e); |
| 110 | + } |
105 | 111 | finally{ |
106 | | - progressBarLock.unlock(); |
| 112 | + progressBarLock.release(); |
107 | 113 | } |
108 | 114 | } |
109 | 115 |
|
|
0 commit comments