<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 on 2011-09-02 15:20 crazy-李陽 閱讀(422) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2011年9月>
    28293031123
    45678910
    11121314151617
    18192021222324
    2526272829301
    2345678

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产综合专区电影在线| 国产V片在线播放免费无码| 男女免费观看在线爽爽爽视频 | 亚洲AV区无码字幕中文色| 亚洲日韩国产一区二区三区在线| 国产精品亚洲片在线| 亚洲国产精品线观看不卡| 免费国产污网站在线观看不要卡| 久久久久国产精品免费免费不卡| 永久黄网站色视频免费直播| 一个人免费观看视频www| 精品亚洲视频在线观看 | 成人在线免费看片| 中国亚洲女人69内射少妇| 中国china体内裑精亚洲日本| 两个人看的www免费视频| 国产精品免费看久久久久| 亚洲一区在线免费观看| 国产成人AV免费观看| 国产免费啪嗒啪嗒视频看看| 亚洲国产成人精品电影| 久久美女网站免费| 99在线精品视频观看免费| 国产亚洲综合一区柠檬导航| 免费观看四虎精品成人| 又黄又爽一线毛片免费观看| 亚洲一本到无码av中文字幕| 69成人免费视频| 亚洲 欧洲 自拍 另类 校园| 无遮免费网站在线入口| 亚洲黄色一级毛片| 亚洲人成免费网站| 亚洲 国产 图片| 一级看片免费视频囗交| 亚洲热线99精品视频| 免费A级毛片无码视频| 免费国内精品久久久久影院| 国产精品亚洲综合天堂夜夜| 午夜视频在线观看免费完整版| 色欲aⅴ亚洲情无码AV蜜桃| 免费国产在线观看老王影院|