<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)專家
    主站蜘蛛池模板: 成人永久福利免费观看| 亚洲精品亚洲人成人网| 色多多www视频在线观看免费| 久久久久亚洲AV成人网人人网站| 未满十八18禁止免费无码网站 | 亚洲精品国产自在久久 | 亚洲依依成人精品| 国产美女精品久久久久久久免费| 国产三级在线免费| 亚洲精品无码专区| 亚洲国产成人片在线观看 | 亚洲激情视频网站| 国产zzjjzzjj视频全免费| 无码午夜成人1000部免费视频| 亚洲乱妇老熟女爽到高潮的片| 亚洲国产另类久久久精品黑人| 免费观看一级毛片| 一级毛片免费毛片一级毛片免费| 美女视频黄.免费网址| 亚洲毛片免费视频| 亚洲一级特黄大片无码毛片| 黄色片在线免费观看| a级片免费观看视频| 婷婷国产偷v国产偷v亚洲| 亚洲精品视频在线播放| 中文字幕亚洲图片| 日本一区免费电影| 无码中文在线二区免费| 免费人成毛片动漫在线播放| 曰批免费视频播放免费| 中国china体内裑精亚洲日本| 亚洲av无码乱码国产精品| 亚洲成a人无码av波多野按摩 | 暖暖日本免费在线视频| 18禁无遮挡无码国产免费网站| 国产日韩精品无码区免费专区国产| 亚洲熟妇无码八V在线播放| 亚洲网站在线播放| 亚洲成a人片在线观看无码| 亚洲情a成黄在线观看| 国产黄色片在线免费观看|