Example usage for com.google.common.util.concurrent ThreadFactoryBuilder setUncaughtExceptionHandler

List of usage examples for com.google.common.util.concurrent ThreadFactoryBuilder setUncaughtExceptionHandler

Introduction

In this page you can find the example usage for com.google.common.util.concurrent ThreadFactoryBuilder setUncaughtExceptionHandler.

Prototype

public ThreadFactoryBuilder setUncaughtExceptionHandler(UncaughtExceptionHandler uncaughtExceptionHandler) 

Source Link

Document

Sets the UncaughtExceptionHandler for new threads created with this ThreadFactory.

Usage

From source file:com.google.worldcoin.wallet.WalletFiles.java

public WalletFiles(final Wallet wallet, File file, long delay, TimeUnit delayTimeUnit) {
    final ThreadFactoryBuilder builder = new ThreadFactoryBuilder().setDaemon(true)
            .setNameFormat("Wallet autosave thread").setPriority(Thread.MIN_PRIORITY); // Avoid competing with the GUI thread.
    Thread.UncaughtExceptionHandler handler = Threading.uncaughtExceptionHandler;
    if (handler != null)
        builder.setUncaughtExceptionHandler(handler);
    // An executor that starts up threads when needed and shuts them down later.
    this.executor = new ScheduledThreadPoolExecutor(1, builder.build());
    this.executor.setKeepAliveTime(5, TimeUnit.SECONDS);
    this.executor.allowCoreThreadTimeOut(true);
    this.wallet = checkNotNull(wallet);
    // File must only be accessed from the auto-save executor from now on, to avoid simultaneous access.
    this.file = checkNotNull(file);
    this.savePending = new AtomicBoolean();
    this.delay = delay;
    this.delayTimeUnit = checkNotNull(delayTimeUnit);

    this.saver = new Callable<Void>() {
        @Override//from   w  w  w .  ja v a 2 s.  co m
        public Void call() throws Exception {
            // Runs in an auto save thread.
            if (!savePending.getAndSet(false)) {
                // Some other scheduled request already beat us to it.
                return null;
            }
            log.info("Background saving wallet, last seen block is {}/{}", wallet.getLastBlockSeenHeight(),
                    wallet.getLastBlockSeenHash());
            saveNowInternal();
            return null;
        }
    };
}

From source file:com.coinomi.core.wallet.WalletFiles.java

public WalletFiles(final Wallet wallet, File file, long delay, TimeUnit delayTimeUnit) {
    final ThreadFactoryBuilder builder = new ThreadFactoryBuilder().setDaemon(true)
            .setNameFormat("Wallet autosave thread").setPriority(Thread.MIN_PRIORITY); // Avoid competing with the GUI thread.
    Thread.UncaughtExceptionHandler handler = Threading.uncaughtExceptionHandler;
    if (handler != null)
        builder.setUncaughtExceptionHandler(handler);
    // An executor that starts up threads when needed and shuts them down later.
    this.executor = new ScheduledThreadPoolExecutor(1, builder.build());
    this.executor.setKeepAliveTime(5, TimeUnit.SECONDS);
    this.executor.allowCoreThreadTimeOut(true);
    this.executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    this.wallet = checkNotNull(wallet, "Cannot save a null wallet");
    // File must only be accessed from the auto-save executor from now on, to avoid simultaneous access.
    this.file = checkNotNull(file, "Cannot save to a null file");
    this.savePending = new AtomicBoolean();
    this.delay = delay;
    this.delayTimeUnit = checkNotNull(delayTimeUnit, "Cannot use a null delay time unit");

    this.saver = new Callable<Void>() {
        @Override//w w w  .  ja  v  a  2 s .c  o m
        public Void call() throws Exception {
            // Runs in an auto save thread.
            if (!savePending.getAndSet(false)) {
                // Some other scheduled request already beat us to it.
                return null;
            }
            log.info("Background saving wallet");
            saveNowInternal();
            return null;
        }
    };
}

From source file:com.reddcoin.core.wallet.WalletFiles.java

public WalletFiles(final Wallet wallet, File file, long delay, TimeUnit delayTimeUnit) {
    final ThreadFactoryBuilder builder = new ThreadFactoryBuilder().setDaemon(true)
            .setNameFormat("Wallet autosave thread").setPriority(Thread.MIN_PRIORITY); // Avoid competing with the GUI thread.
    Thread.UncaughtExceptionHandler handler = Threading.uncaughtExceptionHandler;
    if (handler != null)
        builder.setUncaughtExceptionHandler(handler);
    // An executor that starts up threads when needed and shuts them down later.
    this.executor = new ScheduledThreadPoolExecutor(1, builder.build());
    this.executor.setKeepAliveTime(5, TimeUnit.SECONDS);
    this.executor.allowCoreThreadTimeOut(true);
    this.executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    this.wallet = checkNotNull(wallet);
    // File must only be accessed from the auto-save executor from now on, to avoid simultaneous access.
    this.file = checkNotNull(file);
    this.savePending = new AtomicBoolean();
    this.delay = delay;
    this.delayTimeUnit = checkNotNull(delayTimeUnit);

    this.saver = new Callable<Void>() {
        @Override/*from  w  w  w .  j av a  2  s  .  c om*/
        public Void call() throws Exception {
            // Runs in an auto save thread.
            if (!savePending.getAndSet(false)) {
                // Some other scheduled request already beat us to it.
                return null;
            }
            log.info("Background saving wallet");
            saveNowInternal();
            return null;
        }
    };
}

From source file:com.matthewmitchell.peercoinj.wallet.WalletFiles.java

public WalletFiles(final Wallet wallet, File file, long delay, TimeUnit delayTimeUnit) {
    final ThreadFactoryBuilder builder = new ThreadFactoryBuilder().setDaemon(true)
            .setNameFormat("Wallet autosave thread").setPriority(Thread.MIN_PRIORITY); // Avoid competing with the GUI thread.
    Thread.UncaughtExceptionHandler handler = Threading.uncaughtExceptionHandler;
    if (handler != null)
        builder.setUncaughtExceptionHandler(handler);
    // An executor that starts up threads when needed and shuts them down later.
    this.executor = new ScheduledThreadPoolExecutor(1, builder.build());
    this.executor.setKeepAliveTime(5, TimeUnit.SECONDS);
    this.executor.allowCoreThreadTimeOut(true);
    this.executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    this.wallet = checkNotNull(wallet);
    // File must only be accessed from the auto-save executor from now on, to avoid simultaneous access.
    this.file = checkNotNull(file);
    this.savePending = new AtomicBoolean();
    this.delay = delay;
    this.delayTimeUnit = checkNotNull(delayTimeUnit);

    this.saver = new Callable<Void>() {
        @Override//from  w ww  . j  av a2  s . c  o  m
        public Void call() throws Exception {
            // Runs in an auto save thread.
            if (!savePending.getAndSet(false)) {
                // Some other scheduled request already beat us to it.
                return null;
            }
            log.info("Background saving wallet, last seen block is {}/{}", wallet.getLastBlockSeenHeight(),
                    wallet.getLastBlockSeenHash());
            saveNowInternal();
            return null;
        }
    };
}

From source file:org.diqube.threads.ExecutorManager.java

/**
 * Create a new cached thread pool, see {@link Executors#newCachedThreadPool()}.
 * //from w w w  .  j  ava 2  s.  c o m
 * @param nameFormat
 *          a {@link String#format(String, Object...)}-compatible format String, to which a unique integer (0, 1,
 *          etc.) will be supplied as the single parameter. This integer will be unique to the built instance of the
 *          ThreadFactory and will be assigned sequentially. For example, {@code "rpc-pool-%d"} will generate thread
 *          names like {@code "rpc-pool-0"}, {@code "rpc-pool-1"}, {@code "rpc-pool-2"}, etc.
 * @param uncaughtExceptionHandler
 *          This will be called in case any of the threads of the ExecutorService ends because an exception was
 *          thrown.
 * 
 * @return The new cached thread pool.
 */
public ExecutorService newCachedThreadPool(String nameFormat,
        UncaughtExceptionHandler uncaughtExceptionHandler) {
    ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
    threadFactoryBuilder.setNameFormat(nameFormat);
    threadFactoryBuilder.setUncaughtExceptionHandler(uncaughtExceptionHandler);
    return Executors.newCachedThreadPool(threadFactoryBuilder.build());
}

From source file:org.diqube.threads.ExecutorManager.java

/**
 * Create a new {@link ExecutorService} that does create threads as needed, but contains a maxmimum number of threads.
 * //from  w  w  w.j  a va 2  s  .c o m
 * @param nameFormat
 *          a {@link String#format(String, Object...)}-compatible format String, to which a unique integer (0, 1,
 *          etc.) will be supplied as the single parameter. This integer will be unique to the built instance of the
 *          ThreadFactory and will be assigned sequentially. For example, {@code "rpc-pool-%d"} will generate thread
 *          names like {@code "rpc-pool-0"}, {@code "rpc-pool-1"}, {@code "rpc-pool-2"}, etc.
 * @param uncaughtExceptionHandler
 *          This will be called in case any of the threads of the ExecutorService ends because an exception was
 *          thrown.
 * @param maxPoolSize
 *          Maximum number of threads.
 * @return The new {@link ExecutorService}.
 */
public ExecutorService newCachedThreadPoolWithMax(String nameFormat,
        UncaughtExceptionHandler uncaughtExceptionHandler, int maxPoolSize) {
    ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
    threadFactoryBuilder.setNameFormat(nameFormat);
    threadFactoryBuilder.setUncaughtExceptionHandler(uncaughtExceptionHandler);

    return new ThreadPoolExecutor(0, maxPoolSize, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
            threadFactoryBuilder.build());
}

From source file:org.opendaylight.genius.alivenessmonitor.internal.AlivenessMonitor.java

private ThreadFactory getMonitoringThreadFactory(String threadNameFormat) {
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    builder.setNameFormat(threadNameFormat);
    builder.setUncaughtExceptionHandler(
            (thread, ex) -> LOG.error("Received Uncaught Exception event in Thread: {}", thread.getName(), ex));
    return builder.build();
}

From source file:org.opendaylight.vpnservice.alivenessmonitor.internal.AlivenessMonitor.java

private ThreadFactory getMonitoringThreadFactory(String threadNameFormat) {
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    builder.setNameFormat(threadNameFormat);
    builder.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
        @Override/*  w w  w .j  a v a2  s  . c  om*/
        public void uncaughtException(Thread t, Throwable e) {
            LOG.error("Received Uncaught Exception event in Thread: {}", t.getName(), e);
        }
    });
    return builder.build();
}