1. 程式人生 > >多執行緒使用1

多執行緒使用1

import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <b>類描述:非同步工作管理員</b>TODO<br/>
 * <b>@author:</b>hety<br/>
 * <b>版本資訊:</b>version v1.0.0<br/>
 * <b>日期:</b>2018/12/18 10:00<br/>
 */
public class AsyncScheduleManager {

    /**
     * 執行緒池執行緒名稱字首
     */
    private final String THREAD_POOL_NAME_PREFIX = "AsyncTaskManager-";

    /**
     * 執行緒池預設核心執行緒數
     */
    private final int CORE_POOL_SIZE = 5;
    /**
     * 操作延遲1000毫秒
     */
    private final int OPERATE_DELAY_TIME = 3000;

    /**
     * 非同步操作任務排程執行緒池
     */
    private ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(CORE_POOL_SIZE, new DefaultThreadFactory(THREAD_POOL_NAME_PREFIX));

    /**
     * 單例模式--餓漢方式,類裝載時就例項化me
     */
    private static AsyncScheduleManager me = new AsyncScheduleManager();

    public static AsyncScheduleManager me() {
        return me;
    }

    /**
     * 延時執行任務
     * @param task TimerTask
     */
    public void schedule(TimerTask task)
    {
        executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
    }

    /**
     * 立即執行任務
     * @param task TimerTask
     */
    public void execute(TimerTask task)
    {
        executor.execute(task);
    }



    /**
     * The default thread factory
     */
    static class DefaultThreadFactory implements ThreadFactory {
        private static final AtomicInteger poolNumber = new AtomicInteger(1);
        private final ThreadGroup group;
        private final AtomicInteger threadNumber = new AtomicInteger(1);
        private final String namePrefix;

        /**
         * 構造器
         * @param threadName 執行緒名稱字首
         */
        DefaultThreadFactory(String threadName) {
            SecurityManager s = System.getSecurityManager();
            group = (s != null) ? s.getThreadGroup() :
                    Thread.currentThread().getThreadGroup();
            if (null == threadName || threadName.isEmpty()) {
                threadName = "pool";
            }
            namePrefix = threadName + poolNumber.getAndIncrement() + "-thread-";
        }

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(group, r,
                    namePrefix + threadNumber.getAndIncrement(),
                    0);
            if (t.isDaemon()) {
                t.setDaemon(false);
            }
            if (t.getPriority() != Thread.NORM_PRIORITY) {
                t.setPriority(Thread.NORM_PRIORITY);
            }
            return t;
        }
    }
}

 

import java.util.TimerTask;

/**
 * <b>類描述:</b>TODO<br/>
 * <b>@author:</b>hety<br/>
 * <b>版本資訊:</b>version v1.0.0<br/>
 * <b>日期:</b>2018/12/18 11:34<br/>
 */
public class TaskFactory {

    public static TimerTask msg(Long msg) {
        return new TimerTask() {
            @Override
            public void run() {
                System.out.println(">>>>>>>>>msg:" + (System.currentTimeMillis() - msg)+"\t"+System.currentTimeMillis());
            }
        };
    }
}

測試

....

    public static void test002() throws ExecutionException, InterruptedException {
        AsyncScheduleManager.me().execute(TaskFactory.msg(System.currentTimeMillis()));
        AsyncScheduleManager.me().schedule(TaskFactory.msg(System.currentTimeMillis()));
    }