javacommon.utils.Threads.java Source code

Java tutorial

Introduction

Here is the source code for javacommon.utils.Threads.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package javacommon.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

/**
 * .
 * 
 * @author calvin
 */
public class Threads {

    /**
     * sleep, ??, ???InterruptedException.
     */
    public static void sleep(long durationMillis) {
        try {
            Thread.sleep(durationMillis);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    /**
     * sleep???InterruptedException.
     */
    public static void sleep(long duration, TimeUnit unit) {
        try {
            Thread.sleep(unit.toMillis(duration));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    /**
     * ExecutorService JavaDoc?Graceful Shutdown.
     * 
     * shutdown, ???.
     * 
     * 1/2?, shutdownNow,?workQueuePending,.
     * 
     * 1/2?.
     * 
     * ?shutdown??.
     * 
     * ??.
     */
    public static boolean gracefulShutdown(ExecutorService threadPool, int shutdownTimeoutMills) {
        return MoreExecutors.shutdownAndAwaitTermination(threadPool, shutdownTimeoutMills, TimeUnit.MILLISECONDS);
    }

    /**
     * @see #gracefulShutdown(ExecutorService, int)
     */
    public static boolean gracefulShutdown(ExecutorService threadPool, int shutdownTimeout, TimeUnit timeUnit) {
        return MoreExecutors.shutdownAndAwaitTermination(threadPool, shutdownTimeout, timeUnit);
    }

    /**
     * ThreadFactory???"pool-x-thread-y"
     * 
     * ?"mythread-%d"Guava
     */
    public static ThreadFactory buildThreadFactory(String nameFormat) {
        return new ThreadFactoryBuilder().setNameFormat(nameFormat).build();
    }

    /**
     * ??daemon, daemon, ??, ?daemon.
     * 
     * @see #buildThreadFactory(String)
     */
    public static ThreadFactory buildThreadFactory(String nameFormat, boolean daemon) {
        return new ThreadFactoryBuilder().setNameFormat(nameFormat).setDaemon(daemon).build();
    }

    /**
     * ???ExceptionRunnable??, SchedulerService.
     */
    public static class WrapExceptionRunnable implements Runnable {

        private static Logger logger = LoggerFactory.getLogger(WrapExceptionRunnable.class);

        private Runnable runnable;

        public WrapExceptionRunnable(Runnable runnable) {
            this.runnable = runnable;
        }

        @Override
        public void run() {
            try {
                runnable.run();
            } catch (Throwable e) {
                // catch any exception, because the scheduled thread will break if the exception thrown to outside.
                logger.error("Unexpected error occurred in task", e);
            }
        }
    }

}