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

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

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

    posts - 9, comments - 4, trackbacks - 0, articles - 21

    JDK5 線程池2

    Posted on 2008-01-18 15:18 一步一步努力向上爬 閱讀(293) 評論(0)  編輯  收藏 所屬分類: J2SE學習
     昨天開始研究java的多線程包java.util.concurrent,根據自己的理解實現了一個消息隊列異步調用(200行代碼左右)。拿出來與大家分享我的勞動成果。
      希望大家多提意見。指出哪里寫的不好,以后加以改正。

    ThreadPoolManager類:負責管理線程池,調用輪詢的線程來訪問字符串緩沖區的內容,維護緩沖區,當線程池溢出時拋出的Runnable任務被加入到字符緩沖區。
    public class ThreadPoolManager
    {
        private static ThreadPoolManager tpm = new ThreadPoolManager();

        // 線程池維護線程的最少數量
        private final static int CORE_POOL_SIZE = 4;

        // 線程池維護線程的最大數量
        private final static int MAX_POOL_SIZE = 10;

        // 線程池維護線程所允許的空閑時間
        private final static int KEEP_ALIVE_TIME = 0;

        // 線程池所使用的緩沖隊列大小
        private final static int WORK_QUEUE_SIZE = 10;

        // 消息緩沖隊列
        Queue<String> msgQueue = new LinkedList<String>();

        // 訪問消息緩存的調度線程
        final Runnable accessBufferThread = new Runnable()
        {
            public void run()
            {
                // 查看是否有待定請求,如果有,則創建一個新的AccessDBThread,并添加到線程池中
                if( hasMoreAcquire() )
                {
                    String msg = ( String ) msgQueue.poll();
                    Runnable task = new AccessDBThread( msg );
                    threadPool.execute( task );
                }
            }
        };

        final RejectedExecutionHandler handler = new RejectedExecutionHandler()
        {
            public void rejectedExecution( Runnable r, ThreadPoolExecutor executor )
            {
                System.out.println(((AccessDBThread )r).getMsg()+"消息放入隊列中重新等待執行");
                msgQueue.offer((( AccessDBThread ) r ).getMsg() );
            }
        };

        // 管理數據庫訪問的線程池
        final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,
                new ArrayBlockingQueue( WORK_QUEUE_SIZE ), this.handler );

        // 調度線程池
        final ScheduledExecutorService scheduler = Executors
                .newScheduledThreadPool( 1 );

        final ScheduledFuture taskHandler = scheduler.scheduleAtFixedRate(
                accessBufferThread, 0, 1, TimeUnit.SECONDS );

        public static ThreadPoolManager newInstance()
        {
            return tpm;
        }

        private ThreadPoolManager(){}

        private boolean hasMoreAcquire()
        {
            return !msgQueue.isEmpty();
        }

        public void addLogMsg( String msg )
        {
            Runnable task = new AccessDBThread( msg );
            threadPool.execute( task );
        }
    }

    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()
        {
            // 向數據庫中添加Msg變量值
            System.out.println("Added the message: "+msg+" into the Database");
        }

    }

    public class TestDriver
    {
        ThreadPoolManager tpm = ThreadPoolManager.newInstance();

        public void sendMsg( String msg )
        {
            tpm.addLogMsg( msg + "記錄一條日志 " );
        }

        public static void main( String[] args )
        {
            for( int i = 0; i < 100; i++ )
            {
                new TestDriver().sendMsg( Integer.toString( i ) );
            }
        }
    }
    主站蜘蛛池模板: 无码中文字幕av免费放dvd| 国产成人亚洲精品电影| 羞羞视频在线观看免费| 免费大学生国产在线观看p| 四虎精品视频在线永久免费观看| 亚洲精品字幕在线观看| www永久免费视频| 1000部拍拍拍18免费网站| 亚洲卡一卡2卡三卡4卡无卡三| 亚洲精品无码少妇30P| 免费人成在线视频| 亚洲精品无码久久| 免费人成在线观看视频播放| 日韩久久无码免费毛片软件| 亚洲欧洲久久av| 国内精品免费在线观看| 热99re久久免费视精品频软件| 亚洲欧美第一成人网站7777| 免费中文字幕在线观看| 精品国产福利尤物免费| 成年美女黄网站18禁免费| 国产亚洲高清不卡在线观看| 一级毛片在线免费观看| 亚洲国产精品综合久久2007| 怡红院免费全部视频在线视频| 亚洲成AV人片天堂网无码| 爱情岛论坛免费视频| 亚洲人成人网站色www| 日韩国产欧美亚洲v片| 亚洲精品成人a在线观看| 久久精品国产免费一区| 亚洲一区中文字幕在线电影网| 无码人妻丰满熟妇区免费| 亚洲区精品久久一区二区三区| 日本免费v片一二三区| 免费人成激情视频在线观看冫| 亚洲一区二区三区在线播放| 午夜亚洲国产理论片二级港台二级 | 日本一区二区三区在线视频观看免费| 国产精品亚洲产品一区二区三区| 污污网站18禁在线永久免费观看|