<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-李陽 閱讀(421) | 評論 (0)編輯 收藏

    僅列出標題  
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产精品成人亚洲| 亚洲一卡2卡3卡4卡国产网站 | 国产成人综合亚洲AV第一页 | 国产免费人成视频尤勿视频| 自拍偷自拍亚洲精品被多人伦好爽| 亚洲heyzo专区无码综合| 三年片在线观看免费西瓜视频 | 亚洲成色在线影院| 114级毛片免费观看| 久久亚洲精品成人av无码网站| 一级毛片在线免费看| 亚洲综合男人的天堂色婷婷| 99久久99这里只有免费费精品| 亚洲明星合成图综合区在线| 四虎精品免费永久免费视频| 国产成人免费在线| 亚洲成AV人片久久| 免费黄色网址入口| 一区二区免费国产在线观看| 亚洲精品美女久久777777| 午夜免费啪视频在线观看 | 中文在线观看永久免费| 亚洲国产精品SSS在线观看AV| 免费精品国产自产拍在线观看| 久久久久国产成人精品亚洲午夜 | 亚洲娇小性xxxx| 国产精品免费视频网站| 九九免费观看全部免费视频| 免费中文熟妇在线影片| 久久水蜜桃亚洲AV无码精品| 国产精品亚洲不卡一区二区三区| 久久精品成人免费看| 国产亚洲色视频在线| 99久久久国产精品免费蜜臀| 亚洲Av无码国产一区二区| 亚洲精品国产字幕久久不卡| 精品久久久久久久久免费影院| 无遮挡国产高潮视频免费观看| 亚洲色图校园春色| 日本一道在线日本一道高清不卡免费| 亚洲国产精品日韩在线|