亚洲好骚综合-亚洲黄色录像-亚洲黄色网址-亚洲黄色网址大全-99久久99久久-99久久99久久精品国产

您的位置:軟件測試 > 開源軟件測試 > 開源軟件測試解決方案 >
一道多線程題目的解決方案
作者:網絡轉載 發布時間:[ 2014/4/14 13:37:12 ] 推薦標簽:多線程 解決方案

  在iteye上看到的一道多線程的題目,參考了一下網友的實現,那Eclipse調試通過,算是對JAVA5的并發庫有個大致的了解,分享出來,歡迎園里的同學拍磚。

  題目:

  要求用三個線程,按順序打印1,2,3,4,5.... 71,72,73,74, 75.

  線程1先打印1,2,3,4,5, 然后是線程2打印6,7,8,9,10, 然后是線程3打印11,12,13,14,15。接著再由線程1打印16,17,18,19,20....以此類推, 直到線程3打印到75。

  分析:感覺出題人是要考察一下你是否能夠很好的控制多線程,讓他們有序的進行。

  1、線程池:3個線程,需要使用并發庫的線程池

  2、鎖(lcok):在打印的時候,只允許一個線程進入,其他的線程等待

  下面的主要的代碼:

    import java.util.HashMap; 
    import java.util.Map; 
    import java.util.concurrent.CountDownLatch; 
    import java.util.concurrent.utorService; 
    import java.util.concurrent.utors; 
    import java.util.concurrent.locks.Condition; 
    import java.util.concurrent.locks.Lock; 
    import java.util.concurrent.locks.ReentrantLock; 
    public class NumberPrinter { 
        private Lock lock = new ReentrantLock(); 
        private Condition c1 = lock.newCondition(); 
        private Condition c2 = lock.newCondition(); 
        private Condition c3 = lock.newCondition(); 
        private Map<Integer, Condition> condtionContext =  
            new HashMap<Integer, Condition>(); 
        public NumberPrinter() { 
            condtionContext.put(Integer.valueOf(0), c1); 
            condtionContext.put(Integer.valueOf(1), c2); 
            condtionContext.put(Integer.valueOf(2), c3); 
        } 
         
        private int count = 0;    
         
        public void print(int id) { 
            lock.lock(); 
            try { 
                while(count*5 < 75) { 
                    int curID = calcID(); 
                    if (id == curID) { 
                        for (int i = 1; i<=5; i++) { 
                            System.out.print(count*5 +i+ ","); 
                        } 
                        System.out.println(); 
                        count++; 
                        int nextID = calcID(); 
                        Condition nextCondition = condtionContext.get( 
                                Integer.valueOf(nextID)); 
                        //通知下一線程
                        nextCondition.signal(); 
                    } else { 
                        Condition condition = condtionContext.get( 
                                Integer.valueOf(id)); 
                        condition.await(); 
                    } 
                } 
                //通知線程結束
                for(Condition c : condtionContext.values()) { 
                    c.signal(); 
                } 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } finally { 
                lock.unlock(); 
            } 
        } 
         
        private int calcID() { 
            // TODO Auto-generated method stub
            return count % 3; 
        } 
        /**
         * @param args
         */
        public static void main(String[] args) { 
            utorService executor = utors.newFixedThreadPool(3); 
            final CountDownLatch latch = new CountDownLatch(1);    
            final NumberPrinter printer = new NumberPrinter();  
            for (int i = 0; i < 3; i++) {    
                final int id = i; 
                executor.submit(new Runnable() { 
                    @Override
                    public void run() { 
                        // TODO Auto-generated method stub
                        try { 
                            latch.await(); 
                        } catch (InterruptedException e) { 
                            // TODO Auto-generated catch block
                            e.printStackTrace(); 
                        } 
                        printer.print(id); 
                    } 
                }); 
            } 
            System.out.println("三個任務開始順序打印數字。。。。。。");  
            latch.countDown(); 
            executor.shutdown(); 
        } 
    }

軟件測試工具 | 聯系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd
主站蜘蛛池模板: 免费伦理片网站 | 国产一级久久久久久毛片 | 成人精品一区二区三区中文字幕 | 亚洲精品二区中文字幕 | 欧美成人免费午夜全 | 欧美色精品天天在线观看视频 | 在线免费看毛片 | 欧洲在线观看在线视频吗 | 久久99国产亚洲精品观看 | 看全色黄大色黄大片女图片 | 国产伦码精品一区二区三区 | 亚洲无线码一区二区三区 | 国产丶欧美丶日韩丶不卡影视 | 久爱www成人网免费视频 | 久久九九综合 | 日日夜夜狠狠干 | 欧美在线综合 | 99精品视频在线在线视频观看 | 男女污污无遮挡免费观看 | 国产大片视频免费观看 | 久中文字幕中文字幕亚洲无线 | 精品日韩一区 | 日韩美女一级毛片 | 国产亚洲精品自在线亚洲情侣 | 中文字幕看片在线a免费 | 老妇女毛片| 国产做受视频激情播放 | 啪免费视频| 欧美free性秘书xxxxhd | 国产一二三四区中 | 亚洲va欧美va国产va天堂影 | 欧美本道 | 久久狠狠| 中文字幕在线观看免费视频 | 日韩欧美精品综合久久 | 国产高清在线丝袜精品一区 | 日本综合在线 | 综合玖玖| 午夜视频在线观看网站 | jizzjizz日本老师 | 欧美成人看片一区二区三区 |