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

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

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

    posts - 241,  comments - 116,  trackbacks - 0
    眾所周知hashMap 是線程不安全的,在多線程訪問的情況下,要做同步的處理
    ConcurrentHashMap get() 總能拿到最新的值,類似于關鍵字 volatile保證100%讀取到最新的數據

    如下用代碼說明:

     

    package com.iteye.javaso.demo;

    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;

    public class CopyOfConCurrentHashMapThreadTest2 {

        ConcurrentHashMap<String, String> map = new ConcurrentHashMap<String, String>();

        public static void main(String args[]) {
            CopyOfConCurrentHashMapThreadTest2 test = new CopyOfConCurrentHashMapThreadTest2();
            Runnable sleep = new ThreadSleep2(test, "第一條線程");

            ThreadSecond2 charge2 = new ThreadSecond2(test, "改變值的第2線程");
            ThreadSecond2 charge3 = new ThreadSecond2(test, "改變值的第3線程");
            ThreadSecond2 charge4 = new ThreadSecond2(test, "改變值的第4線程");

            ThreadSecond23 chargeXX = new ThreadSecond23(test, "改變值的XXXX線程");

            ExecutorService exc = Executors.newFixedThreadPool(5);

            exc.execute(sleep);

            exc.execute(charge3);
            exc.execute(charge2);
            exc.execute(chargeXX);
            exc.execute(charge4);
            exc.shutdown();
            System.out.println("洗洗睡吧----------------");
            try {
                Thread.sleep(16000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("多個線程后,最終運行結果:" + test.map.get("firstKey"));
        }

        public  void put(String value, int state) {
            map.put("firstKey", value);

            // Thread thread= Thread.currentThread();
            if (state == 0) {
                System.out.println("開始睡覺了--------------");
                try {
                    Thread.sleep(4000);
                    System.out.println("睡醒了-------");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }

    }

    class ThreadSleep2 extends Thread {

        ConcurrentHashMap<String, String> map;

        public ThreadSleep2(ConcurrentHashMap<String, String> map, String threadName) {

            this.map = map;
            this.name = threadName;
        }

        CopyOfConCurrentHashMapThreadTest2 test;

        public ThreadSleep2(CopyOfConCurrentHashMapThreadTest2 test,
                String threadName) {

            super(threadName);
            this.test = test;
            this.name = threadName;
        }

        @Override
        public void run() {
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            System.out.println("---------------進入第一條線程-----睡十秒先--------");

            System.out.println("第一條線程未設置前值為:*** " + test.map.get("firstKey"));

            test.put(name, 0);

            System.out.println("第一條線程執(zhí)行完畢  Map中值為:" +test.map.get("firstKey"));

        }

        String name = "";

        public String toString() {

            return "當前線程的名字是:" + name;
        }

    }

    class ThreadSecond2 extends Thread {

        ConcurrentHashMap<String, String> map;

        public ThreadSecond2(ConcurrentHashMap<String, String> map,
                String threadName) {
            super(threadName);
            this.map = map;
            this.name = threadName;
        }

        CopyOfConCurrentHashMapThreadTest2 test;

        public ThreadSecond2(CopyOfConCurrentHashMapThreadTest2 test,
                String threadName) {

            super(threadName);
            this.test = test;
            this.name = threadName;
        }

        @Override
        public void run() {
            System.out.println("-----------進入其它線程---------");
            System.out.println("當前線程是:" + this.name + " 未設置map前值為:"
                    + test.map.get("firstKey"));

            test.put(name, 2);
            System.out.println("hashMap中 firstKey值為:" + name);
        }

        String name = "";

        public String toString() {

            return "當前線程的名字是:" + name;
        }
    商賬追收


    }

    class ThreadSecond23 extends Thread {

        ConcurrentHashMap<String, String> map;

        public ThreadSecond23(ConcurrentHashMap<String, String> map,
                String threadName) {
            super(threadName);
            this.map = map;
            this.name = threadName;
        }

        CopyOfConCurrentHashMapThreadTest2 test;

        public ThreadSecond23(CopyOfConCurrentHashMapThreadTest2 test,
                String threadName) {

            super(threadName);
            this.test = test;
            this.name = threadName;
        }

        @Override
        public void run() {
            System.out.println("-----------進入XXXXXXX線程---------");
            System.out.println("當前線程是:" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            test.put(name, 2);
            System.out.println("hashMap中 firstKey值為:" + name);
        }

        String name = "";

        public String toString() {

            return "當前線程的名字是:" + name;
        }

    }
    posted on 2011-06-03 09:28 墻頭草 閱讀(766) 評論(0)  編輯  收藏

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


    網站導航:
     
    人人游戲網 軟件開發(fā)網 貨運專家
    主站蜘蛛池模板: 久久丫精品国产亚洲av不卡| 亚洲日韩精品无码专区网址| 亚洲av无码电影网| 99久久久国产精品免费牛牛四川 | 亚洲男人天堂2020| 黄床大片30分钟免费看| 四虎影视精品永久免费网站| 自拍偷自拍亚洲精品播放| 在线免费观看韩国a视频| 老妇激情毛片免费| heyzo亚洲精品日韩| 成人网站免费大全日韩国产| 亚洲精品无码久久千人斩| 精品国产免费一区二区三区香蕉 | 亚洲人成电影网站久久| 成年女人毛片免费播放视频m| 自拍日韩亚洲一区在线| 国产片免费在线观看| 国产成人无码精品久久久久免费| 色噜噜AV亚洲色一区二区| 久久中文字幕免费视频| 精品亚洲AV无码一区二区 | 久久嫩草影院免费看夜色| 久久亚洲精品视频| 在线视频精品免费| 亚洲成av人片在线天堂无| 亚洲一区视频在线播放| 久久综合九色综合97免费下载| 亚洲老熟女@TubeumTV| 成人网站免费观看| 九九全国免费视频| 亚洲人成在线播放网站岛国| 国产免费看JIZZ视频| 特级aaaaaaaaa毛片免费视频| 亚洲女久久久噜噜噜熟女| **俄罗斯毛片免费| 久久水蜜桃亚洲AV无码精品| 亚洲中文久久精品无码| 国产在线jyzzjyzz免费麻豆 | 日产乱码一卡二卡三免费| 99久久99这里只有免费的精品 |