<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    置頂隨筆

    [置頂]線程池代碼

    ThreadPoolManager

    package com.threadpool.test;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.Executors;
    import java.util.concurrent.RejectedExecutionException;
    import java.util.concurrent.RejectedExecutionHandler;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.ScheduledFuture;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;

    import org.omg.CORBA.TIMEOUT;

    import com.nio.test.ReadFileThread;

    public class ThreadPoolManager {

        private static ThreadPoolManager tpm = new ThreadPoolManager();

        // 線程池最小線程數
        private final static int MIN_SIZE = 4;
        // 線程池最大線程數
        private final static int MAX_SIZE = 10;
        // 線程池維護線程允許的空閑限制
        private final static int KEEP_ACTIVE_TIME = 0;
        // 線程池用的緩沖隊列大小
        private final static int WORK_QUEUE_SIZE = 10;
        // 消息緩沖隊列
        Queue queue = new LinkedList();
        
        final Runnable accessBuffeThread = new Runnable()
        {

            public void run() {
                
                if( hasMoreAcquire() ){
                    
                    String msg = ( String ) queue.poll();
                    Runnable task = new AccessDBThread( msg );
                    threadpool.execute( task );
                }
            }
            
        };
        // 無法由 ThreadPoolExecutor 執行的任務的處理程序
        final RejectedExecutionHandler handler = new RejectedExecutionHandler()
        {

            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                // TODO Auto-generated method stub
                System.out.println(((AccessDBThread )r).getMsg()+"消息放入隊列中重新等待執行");
                queue.offer((( AccessDBThread ) r ).getMsg() );
            }
            
        };
        
        final ThreadPoolExecutor threadpool = new ThreadPoolExecutor(MIN_SIZE, MAX_SIZE, KEEP_ACTIVE_TIME, TimeUnit.SECONDS, new ArrayBlockingQueue(WORK_QUEUE_SIZE),this.handler);
        
        // 調度線程池

        final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool( 1 );

        final ScheduledFuture taskHandler = scheduler.scheduleAtFixedRate(accessBuffeThread, 0, 1, TimeUnit.SECONDS);
        
        public static ThreadPoolManager newinstance()
        {
            return tpm;
        }
        
        private ThreadPoolManager (){}
        
        
        private boolean hasMoreAcquire()
        {
            return !queue.isEmpty();
        }
        
        public void addLogMsg(String msg)
        {
         Runnable task = new AccessDBThread(msg);
         threadpool.execute(task);
        }
    }
    AccessDBThread
    package com.threadpool.test;

    public class AccessDBThread implements Runnable{

        private String msg;
        public String getMsg() {
            return msg;
        }
        public void setMsg(String msg) {
            this.msg = msg;
        }
        
        public AccessDBThread(){
            super();
        }
        
        public AccessDBThread(String msg)
        {
            this.msg=msg;
        }
        public void run() {
            // TODO Auto-generated method stub
            System.out.println("Added the message: "+msg+" into the Database");
        }

        
    }

    測試類:
    package com.threadpool.test;


    public class TestDriver {

        ThreadPoolManager tpm = ThreadPoolManager.newinstance();
        
        public void addMsg(String msg)
        {
            tpm.addLogMsg(msg);
        }
        public static void main(String[] args) {
            for(int i=0;i<100;i++)
            {
                new TestDriver().addMsg(Integer.toString(i));
            }
        }
    }



    posted @ 2011-09-02 15:20 crazy-李陽 閱讀(417) | 評論 (0)編輯 收藏

    僅列出標題  
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 日本不卡免费新一二三区| 97无码免费人妻超级碰碰碰碰| 免费国产成人午夜私人影视| 亚洲高清一区二区三区电影| 亚洲男人天堂av| 日韩精品无码免费专区午夜 | 精品久久久久久久免费人妻| 国产精品亚洲自在线播放页码| 野花高清在线观看免费3中文| 456亚洲人成影院在线观| 欧美男同gv免费网站观看| 91丁香亚洲综合社区| 精品免费国产一区二区三区| 国产精品亚洲一区二区三区久久 | 精品亚洲一区二区三区在线观看| 亚洲人成毛片线播放| 日韩亚洲国产高清免费视频| 天堂亚洲国产中文在线| 国产色爽免费视频| 一级特黄a大片免费| 亚洲av无码无在线观看红杏| 91精品视频在线免费观看| 亚洲一区二区三区免费在线观看| 四虎影视www四虎免费| 国产亚洲精品精品精品| 亚洲啪啪AV无码片| 亚洲日本在线免费观看| 亚洲hairy多毛pics大全| 久久夜色精品国产亚洲av| 91热久久免费精品99| 亚洲精品无码成人| 久久亚洲色一区二区三区| 久久久久久精品成人免费图片| 欧洲亚洲国产清在高| 国产精品入口麻豆免费观看| 国产天堂亚洲国产碰碰| 久久精品亚洲综合| 国产在线19禁免费观看国产| 久久青青草原国产精品免费| 亚洲人片在线观看天堂无码| 久久精品国产亚洲麻豆|