<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 是線程不安全的,在多線程訪問(wèn)的情況下,要做同步的處理
    ConcurrentHashMap get() 總能拿到最新的值,類似于關(guān)鍵字 volatile保證100%讀取到最新的數(shù)據(jù)

    如下用代碼說(shuō)明:

     

    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("多個(gè)線程后,最終運(yùn)行結(jié)果:" + 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("開(kāi)始睡覺(jué)了--------------");
                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("---------------進(jìn)入第一條線程-----睡十秒先--------");

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

            test.put(name, 0);

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

        }

        String name = "";

        public String toString() {

            return "當(dāng)前線程的名字是:" + 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("-----------進(jìn)入其它線程---------");
            System.out.println("當(dāng)前線程是:" + this.name + " 未設(shè)置map前值為:"
                    + test.map.get("firstKey"));

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

        String name = "";

        public String toString() {

            return "當(dāng)前線程的名字是:" + 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("-----------進(jìn)入XXXXXXX線程---------");
            System.out.println("當(dāng)前線程是:" + 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 "當(dāng)前線程的名字是:" + name;
        }

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

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


    網(wǎng)站導(dǎo)航:
     
    人人游戲網(wǎng) 軟件開(kāi)發(fā)網(wǎng) 貨運(yùn)專家
    主站蜘蛛池模板: 欧洲人免费视频网站在线| 亚洲大片免费观看| 亚洲国产欧美日韩精品一区二区三区| 99视频在线观看免费| 99在线精品免费视频九九视| 久久久久久亚洲av成人无码国产| 中文字幕免费人成乱码中国| 成年美女黄网站18禁免费 | 红杏亚洲影院一区二区三区| 亚洲伊人久久大香线焦| 一区二区三区免费在线观看| 国产成人亚洲综合| 亚洲国产区男人本色| 日韩中文无码有码免费视频| 亚洲精品无码MV在线观看| 99re6在线视频精品免费| 亚洲日韩国产精品第一页一区| 精品视频一区二区三区免费| 久久久久久亚洲精品成人| 一级女人18毛片免费| 亚洲一区二区观看播放| 99久久人妻精品免费二区| 亚洲成AV人片久久| 无人在线观看免费高清| 亚洲视频一区在线观看| 成人毛片免费观看| 日韩免费码中文在线观看| 亚洲精品自在在线观看| 国产91免费在线观看| 亚洲AV无码专区在线观看成人| 久草视频免费在线| 亚洲欧美精品午睡沙发| 亚洲精品第一国产综合精品99| 色天使亚洲综合一区二区| 国产AⅤ无码专区亚洲AV| 一级毛片高清免费播放| 97久久精品亚洲中文字幕无码 | a毛片免费观看完整| 亚洲免费在线视频播放| 亚洲精品无码永久在线观看| 色猫咪免费人成网站在线观看|