mirror of
https://github.com/bisq-network/bisq.git
synced 2025-03-04 03:03:48 +01:00
Factor out executor shutdown logic into new Utilities method
This commit is contained in:
parent
b7c7eaf1f2
commit
863392bc74
4 changed files with 22 additions and 37 deletions
|
@ -126,11 +126,11 @@ public class Utilities {
|
|||
new ArrayBlockingQueue<>(maximumPoolSize));
|
||||
}
|
||||
|
||||
public static ThreadPoolExecutor getThreadPoolExecutor(String name,
|
||||
int corePoolSize,
|
||||
int maximumPoolSize,
|
||||
long keepAliveTimeInSec,
|
||||
BlockingQueue<Runnable> workQueue) {
|
||||
private static ThreadPoolExecutor getThreadPoolExecutor(String name,
|
||||
int corePoolSize,
|
||||
int maximumPoolSize,
|
||||
long keepAliveTimeInSec,
|
||||
BlockingQueue<Runnable> workQueue) {
|
||||
final ThreadFactory threadFactory = new ThreadFactoryBuilder()
|
||||
.setNameFormat(name)
|
||||
.setDaemon(true)
|
||||
|
@ -142,7 +142,6 @@ public class Utilities {
|
|||
return executor;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("SameParameterValue")
|
||||
public static ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor(String name,
|
||||
int corePoolSize,
|
||||
|
@ -162,6 +161,18 @@ public class Utilities {
|
|||
return executor;
|
||||
}
|
||||
|
||||
// TODO: Can some/all of the uses of this be replaced by guava MoreExecutors.shutdownAndAwaitTermination(..)?
|
||||
public static void shutdownAndAwaitTermination(ExecutorService executor, long timeout, TimeUnit unit) {
|
||||
executor.shutdown();
|
||||
try {
|
||||
if (!executor.awaitTermination(timeout, unit)) {
|
||||
executor.shutdownNow();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
executor.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if <code>defaults read -g AppleInterfaceStyle</code> has an exit status of <code>0</code> (i.e. _not_ returning "key not found").
|
||||
*/
|
||||
|
|
|
@ -20,7 +20,6 @@ import java.io.IOException;
|
|||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
|
@ -79,8 +78,8 @@ public class BitcoindDaemon {
|
|||
} catch (IOException e) {
|
||||
log.error("Error closing block notification server socket", e);
|
||||
} finally {
|
||||
shutdownAndAwaitTermination(executor, 1);
|
||||
shutdownAndAwaitTermination(workerPool, 5);
|
||||
Utilities.shutdownAndAwaitTermination(executor, 1, TimeUnit.SECONDS);
|
||||
Utilities.shutdownAndAwaitTermination(workerPool, 5, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,17 +100,6 @@ public class BitcoindDaemon {
|
|||
};
|
||||
}
|
||||
|
||||
private static void shutdownAndAwaitTermination(ExecutorService executor, long timeoutInSeconds) {
|
||||
executor.shutdown();
|
||||
try {
|
||||
if (!executor.awaitTermination(timeoutInSeconds, TimeUnit.SECONDS)) {
|
||||
executor.shutdownNow();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
executor.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
public interface BlockListener {
|
||||
void blockDetected(String blockHash);
|
||||
}
|
||||
|
|
|
@ -78,14 +78,6 @@ public class PriceRequest {
|
|||
if (provider != null) {
|
||||
provider.shutDown();
|
||||
}
|
||||
|
||||
executorService.shutdown();
|
||||
try {
|
||||
if (!executorService.awaitTermination(1, TimeUnit.SECONDS)) {
|
||||
executorService.shutdownNow();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
executorService.shutdownNow();
|
||||
}
|
||||
Utilities.shutdownAndAwaitTermination(executorService, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package bisq.monitor;
|
||||
|
||||
import bisq.common.app.Version;
|
||||
import bisq.common.util.Utilities;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.Random;
|
||||
|
@ -141,13 +142,6 @@ public abstract class Metric extends Configurable implements Runnable {
|
|||
* shut down or after one minute.
|
||||
*/
|
||||
public static void haltAllMetrics() {
|
||||
executor.shutdown();
|
||||
|
||||
try {
|
||||
if (!Metric.executor.awaitTermination(2, TimeUnit.MINUTES))
|
||||
Metric.executor.shutdownNow();
|
||||
} catch (InterruptedException e) {
|
||||
Metric.executor.shutdownNow();
|
||||
}
|
||||
Utilities.shutdownAndAwaitTermination(executor, 2, TimeUnit.MINUTES);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue