common-utils/src/main/java/org/warp/commonutils/concurrency/executor/PermitReleasingCallableDecorator.java

35 lines
808 B
Java
Raw Normal View History

2020-08-24 23:48:05 +02:00
package org.warp.commonutils.concurrency.executor;
import java.util.concurrent.Callable;
2020-10-16 19:50:30 +02:00
import org.jetbrains.annotations.NotNull;
2020-08-24 23:48:05 +02:00
public final class PermitReleasingCallableDecorator<T> extends CallableDecorator<T> {
2020-10-16 19:50:30 +02:00
@NotNull
2020-09-17 18:00:34 +02:00
private final QueueSizeUpdater queueSizeUpdater;
2020-08-24 23:48:05 +02:00
2020-10-16 19:50:30 +02:00
PermitReleasingCallableDecorator(@NotNull final Callable<T> task,
@NotNull final QueueSizeUpdater queueSizeUpdater) {
2020-08-24 23:48:05 +02:00
super(task);
this.queueSizeUpdater = queueSizeUpdater;
}
@Override
public T call() throws Exception {
try {
2020-09-17 18:00:34 +02:00
queueSizeUpdater.update(true);
2020-08-24 23:48:05 +02:00
} finally {
2020-09-17 18:00:34 +02:00
try {
return super.call();
} finally {
queueSizeUpdater.update(false);
}
2020-08-24 23:48:05 +02:00
}
}
@Override
public final String toString() {
return String.format("%s[delegate='%s']", getClass().getSimpleName(), super.toString());
}
}