zoukankan      html  css  js  c++  java
  • 多线程工具类与应用

    多线程工具类:

    /**
     * ThreadPoolUtil.java
     */
    package com.lbs.util;
    
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.RejectedExecutionHandler;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
    import java.util.concurrent.TimeUnit;
    /**
     * 
     * <p>ClassName: ThreadPoolUtil</p>
     * <p>Description: TODO</p>
     */
    public class ThreadPoolUtil {
        
        
        /**
         * 设置默认初始线程池的大小
         */
        private static final int COREPOOLSIZE = 24;
        /**
         * MAXIMUMPOOLSIZE
         */
        private static final int MAXIMUMPOOLSIZE = 100;
        /**
         * QUEUESIZE
         */
        private static final int QUEUESIZE = 2000;
        /**
         * KEEPLIVETIME
         */
        private static final long KEEPLIVETIME = 5L;
        /**
         * queue
         */
        private static final BlockingQueue<Runnable> QUEUE = new ArrayBlockingQueue<Runnable>(QUEUESIZE);
        /**
         * CALLERRUNSHANDLER
         */
        private static final RejectedExecutionHandler CALLERRUNSHANDLER = new CallerRunsPolicy();
        /**
         * instance
         */
        private static ThreadPoolUtil instance = null;
        /**
         * boundedThreadPool
         */
        private ExecutorService boundedThreadPool;
        /**
         * 
         * <p>Description: TODO</p>
         */
        private ThreadPoolUtil() {
            // 创建有界大小的线程池
            this.boundedThreadPool = new ThreadPoolExecutor(COREPOOLSIZE, MAXIMUMPOOLSIZE, KEEPLIVETIME, TimeUnit.SECONDS,
                    QUEUE, CALLERRUNSHANDLER);
        }
        /**
         * 提供唯一实例
         * 
         * @return instance
         */
        public static ThreadPoolUtil getInstance() {
            if (instance == null) {
                synchronized (ThreadPoolUtil.class) {
                    if (instance == null) {
                        instance = new ThreadPoolUtil();
                    }
                }
            }
            return instance;
        }
    
        /**
         * 
         * <p>Description: TODO</p>
         * @return ExecutorService
         */
        public ExecutorService getBoundedThreadPool() {
            return this.boundedThreadPool;
        }
    }

    应用举例:

  • 相关阅读:
    DateTime类型的一个Bug
    无痛苦的软件维护——被遗忘的需求
    完全命令行.NET开发
    无痛苦的软件维护——文档和代码
    .NET初学者架构设计指南(一)Hello world的时代
    NGOSS的一点简单概念
    软件的逻辑层次
    VSTS for Testers学习笔记目录
    How Google Tests Software (出书,停止更新)
    推荐——《浪潮之巅》(据传稍后会出书,停止更新)
  • 原文地址:https://www.cnblogs.com/alphajuns/p/13043283.html
Copyright © 2011-2022 走看看