mirror of
https://github.com/bisq-network/bisq.git
synced 2025-03-04 11:07:59 +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));
|
new ArrayBlockingQueue<>(maximumPoolSize));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ThreadPoolExecutor getThreadPoolExecutor(String name,
|
private static ThreadPoolExecutor getThreadPoolExecutor(String name,
|
||||||
int corePoolSize,
|
int corePoolSize,
|
||||||
int maximumPoolSize,
|
int maximumPoolSize,
|
||||||
long keepAliveTimeInSec,
|
long keepAliveTimeInSec,
|
||||||
BlockingQueue<Runnable> workQueue) {
|
BlockingQueue<Runnable> workQueue) {
|
||||||
final ThreadFactory threadFactory = new ThreadFactoryBuilder()
|
final ThreadFactory threadFactory = new ThreadFactoryBuilder()
|
||||||
.setNameFormat(name)
|
.setNameFormat(name)
|
||||||
.setDaemon(true)
|
.setDaemon(true)
|
||||||
|
@ -142,7 +142,6 @@ public class Utilities {
|
||||||
return executor;
|
return executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("SameParameterValue")
|
@SuppressWarnings("SameParameterValue")
|
||||||
public static ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor(String name,
|
public static ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor(String name,
|
||||||
int corePoolSize,
|
int corePoolSize,
|
||||||
|
@ -162,6 +161,18 @@ public class Utilities {
|
||||||
return executor;
|
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").
|
* @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.ArrayBlockingQueue;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
@ -79,8 +78,8 @@ public class BitcoindDaemon {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Error closing block notification server socket", e);
|
log.error("Error closing block notification server socket", e);
|
||||||
} finally {
|
} finally {
|
||||||
shutdownAndAwaitTermination(executor, 1);
|
Utilities.shutdownAndAwaitTermination(executor, 1, TimeUnit.SECONDS);
|
||||||
shutdownAndAwaitTermination(workerPool, 5);
|
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 {
|
public interface BlockListener {
|
||||||
void blockDetected(String blockHash);
|
void blockDetected(String blockHash);
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,14 +78,6 @@ public class PriceRequest {
|
||||||
if (provider != null) {
|
if (provider != null) {
|
||||||
provider.shutDown();
|
provider.shutDown();
|
||||||
}
|
}
|
||||||
|
Utilities.shutdownAndAwaitTermination(executorService, 1, TimeUnit.SECONDS);
|
||||||
executorService.shutdown();
|
|
||||||
try {
|
|
||||||
if (!executorService.awaitTermination(1, TimeUnit.SECONDS)) {
|
|
||||||
executorService.shutdownNow();
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
executorService.shutdownNow();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
package bisq.monitor;
|
package bisq.monitor;
|
||||||
|
|
||||||
import bisq.common.app.Version;
|
import bisq.common.app.Version;
|
||||||
|
import bisq.common.util.Utilities;
|
||||||
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
@ -141,13 +142,6 @@ public abstract class Metric extends Configurable implements Runnable {
|
||||||
* shut down or after one minute.
|
* shut down or after one minute.
|
||||||
*/
|
*/
|
||||||
public static void haltAllMetrics() {
|
public static void haltAllMetrics() {
|
||||||
executor.shutdown();
|
Utilities.shutdownAndAwaitTermination(executor, 2, TimeUnit.MINUTES);
|
||||||
|
|
||||||
try {
|
|
||||||
if (!Metric.executor.awaitTermination(2, TimeUnit.MINUTES))
|
|
||||||
Metric.executor.shutdownNow();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Metric.executor.shutdownNow();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue