Skip to content

Commit bbd7eda

Browse files
committed
Use semaphore instead of lock
1 parent 2314b79 commit bbd7eda

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

src/main/java/fr/rakambda/mediaconverter/progress/ConversionProgressExecutor.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@
1313
import java.util.concurrent.CompletionException;
1414
import java.util.concurrent.ExecutorService;
1515
import java.util.concurrent.Future;
16+
import java.util.concurrent.Semaphore;
1617
import java.util.concurrent.TimeUnit;
17-
import java.util.concurrent.locks.ReentrantLock;
1818

1919
@Log4j2
2020
public class ConversionProgressExecutor implements ExecutorService, AutoCloseable{
2121
private final ExecutorService delegate;
2222
private final ProgressBar progressBar;
23-
private final ReentrantLock progressBarLock;
23+
private final Semaphore progressBarLock;
2424

2525
public ConversionProgressExecutor(@NonNull ExecutorService executorService){
2626
delegate = executorService;
2727
progressBar = new ProgressBarBuilder()
2828
.setTaskName("Conversion")
2929
.setInitialMax(-1)
3030
.build();
31-
progressBarLock = new ReentrantLock();
31+
progressBarLock = new Semaphore(1);
3232
}
3333

3434
public static ConversionProgressExecutor of(@NonNull ExecutorService executorService){
@@ -88,22 +88,28 @@ public <T> Future<T> submit(@NonNull Callable<T> task){
8888
}
8989

9090
private void incrementProgressbarMax(){
91-
progressBarLock.lock();
9291
try{
92+
progressBarLock.acquire();
9393
progressBar.maxHint(progressBar.getMax() + 1);
9494
}
95+
catch(InterruptedException e){
96+
throw new RuntimeException(e);
97+
}
9598
finally{
96-
progressBarLock.unlock();
99+
progressBarLock.release();
97100
}
98101
}
99102

100103
private void stepProgressBar(long amount){
101-
progressBarLock.lock();
102104
try{
105+
progressBarLock.acquire();
103106
progressBar.stepBy(amount);
104107
}
108+
catch(InterruptedException e){
109+
throw new RuntimeException(e);
110+
}
105111
finally{
106-
progressBarLock.unlock();
112+
progressBarLock.release();
107113
}
108114
}
109115

0 commit comments

Comments
 (0)