<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("第一條線程執行完畢  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 墻頭草 閱讀(771) 評論(0)  編輯  收藏

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


    網站導航:
     
    人人游戲網 軟件開發網 貨運專家
    主站蜘蛛池模板: 亚洲综合伊人制服丝袜美腿| 亚洲人成高清在线播放| 亚洲人成色7777在线观看不卡| 亚洲 综合 国产 欧洲 丝袜| 亚洲国产精品尤物yw在线| 亚洲国产一区国产亚洲| 亚洲国产成人久久精品app| 今天免费中文字幕视频| 日本免费中文字幕在线看| 水蜜桃亚洲一二三四在线| 国产精品亚洲二区在线| 无码国产精品一区二区免费模式 | 久草免费手机视频| 成人免费淫片在线费观看| 久久亚洲欧洲国产综合| 久久精品国产亚洲αv忘忧草| 特级毛片爽www免费版| 91精品免费国产高清在线| 亚洲中文字幕无码专区| 亚洲国产系列一区二区三区| 精品视频一区二区三区免费| 午夜影视日本亚洲欧洲精品一区 | 久久亚洲精精品中文字幕| 污污视频网站免费观看| 日韩吃奶摸下AA片免费观看| 久久久久亚洲AV无码专区首| 日韩人妻无码精品久久免费一 | 国产成人va亚洲电影| 亚洲色图综合在线| 免费视频一区二区| 亚洲乱码在线视频| 国产在线a不卡免费视频| 亚洲另类自拍丝袜第五页 | 在线aⅴ亚洲中文字幕| 最刺激黄a大片免费网站| 亚洲免费观看视频| 夜夜爽妓女8888视频免费观看| 亚洲中文字幕第一页在线 | 亚洲综合激情六月婷婷在线观看| 中文字幕乱码免费看电影| 亚洲中文字幕无码不卡电影|