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

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

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

    posts - 1,  comments - 25,  trackbacks - 0

        ThreadLocal的核心思想很簡單:為每個獨(dú)立的線程提供一個變量的副本。

        ThreadLocal則使用了“拷貝副本”的方式,人人有份,你用你的,我用我的,大家互不影響,是“以空間換時間”。每個線程修改變量時,實(shí)際上修改的是變量的副本,不怕影響到其它線程。

        為了加深對ThreadLocal的理解,下面我使用一個例子來演示ThreadLocal如何隔離線程間的變量訪問和修改:

    【1】SerialNum類

    package example.thread.threadLocal;

    public class SerialNum {

        private static int nextSerialNum = 1;

        @SuppressWarnings("unchecked")
        private static ThreadLocal serialNum = new ThreadLocal() {
            protected synchronized Object initialValue() {
                return new Integer(nextSerialNum++);      
            }                                                           
        };

        public static int get() {
            return ((Integer) (serialNum.get())).intValue();
        }
        
        @SuppressWarnings("unchecked")
        public static void set(Integer newSerial){
            serialNum.set(newSerial);
        }
    }

    【2】GetSerialNumThread
    package example.thread.threadLocal;

    public class GetSerialNumThread implements Runnable {

        public static void main(String args[]) {

            GetSerialNumThread serialNumGetter = new GetSerialNumThread();
            Thread t1 = new Thread(serialNumGetter, "Thread A");
            Thread t2 = new Thread(serialNumGetter, "Thread B");
            t1.start();
            try {
                t1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }    
            t2.start();            
        }

        public void run() {
            int mySerialNum = getSerialNum();
            System.out.println("線程 " + Thread.currentThread().getName()
                    + " 獲取到的序列號是" + mySerialNum);
            System.out.println("線程 " + Thread.currentThread().getName()
                    + " 修改了序列號為" + (mySerialNum * 3));
            setSerialNum(mySerialNum * 3);
            System.out.println("線程 " + Thread.currentThread().getName()
                    + " 再次獲得的序列號是" + getSerialNum());
        }

        private int getSerialNum() {
            return SerialNum.get();
        }

        private void setSerialNum(int newSerialNum) {
            SerialNum.set(new Integer(newSerialNum));
        }
    }

    運(yùn)行的結(jié)果如下:
    線程 Thread A 獲取到的序列號是1
    線程 Thread A 修改了序列號為3
    線程 Thread A 再次獲得的序列號是3
    線程 Thread B 獲取到的序列號是2
    線程 Thread B 修改了序列號為6
    線程 Thread B 再次獲得的序列號是6

        可見第一個線程在調(diào)用SerialNum.set(int)方法修改static變量時,其實(shí)修改的是它自己的副本,而不是修改本地變量,第二個線程在初始化的時候拿到的序列號是2而不是7。

        為什么會這樣呢?明明serialNum是靜態(tài)變量啊?其實(shí)我們只需要看看ThreadLocal的內(nèi)部構(gòu)造就知道了:

    A. ThreadLocal的get()方法:
     /**
         * Returns the value in the current thread's copy of this thread-local
         * variable.  Creates and initializes the copy if this is the first time
         * the thread has called this method.
         *
         * @return the current thread's value of this thread-local
         */
        public T get() {
            Thread t = Thread.currentThread();
            ThreadLocalMap map = getMap(t);
            if (map != null)
                return (T)map.get(this);

            // Maps are constructed lazily.  if the map for this thread
            // doesn't exist, create it, with this ThreadLocal and its
            // initial value as its only entry.
            T value = initialValue();
            createMap(t, value);
            return value;
        }

    B. ThreadLocal的set()方法:
    /**
         * Sets the current thread's copy of this thread-local variable
         * to the specified value.  Many applications will have no need for
         * this functionality, relying solely on the {@link #initialValue}
         * method to set the values of thread-locals.
         *
         * @param value the value to be stored in the current threads' copy of
         *        this thread-local.
         */
        public void set(T value) {
            Thread t = Thread.currentThread();
            ThreadLocalMap map = getMap(t);
            if (map != null)
                map.set(this, value);
            else
                createMap(t, value);
        }

        可以看到ThreadLocal在內(nèi)部維護(hù)了一個Map,將變量的值和線程綁定起來,get/set方法都是對該線程對應(yīng)的value進(jìn)行操作,所以不會影響到其它線程。
    posted on 2010-12-09 19:27 Daniel 閱讀(209) 評論(0)  編輯  收藏 所屬分類: CoreJava
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    留言簿(3)

    隨筆檔案

    文章分類

    文章檔案

    相冊

    搜索

    •  

    最新評論

    主站蜘蛛池模板: 高潮内射免费看片| 日本一卡精品视频免费| 亚洲精品午夜无码专区| 最近中文字幕mv免费高清视频8| 成人亚洲国产va天堂| 中文字幕精品亚洲无线码一区| 97视频免费观看2区| 免费观看亚洲人成网站| 亚洲最新视频在线观看| 日本免费一区二区三区最新vr| 成人自慰女黄网站免费大全| 成人区精品一区二区不卡亚洲| 亚洲综合另类小说色区| 妞干网在线免费视频| 久久免费线看线看| 黄色一级免费网站| 亚洲成无码人在线观看| 伊人久久大香线蕉亚洲五月天| 黄色片在线免费观看| WWW免费视频在线观看播放| 91丁香亚洲综合社区| 亚洲av无码乱码国产精品| 国产一级一片免费播放i| 日本免费人成在线网站| 99久久免费国产精精品| jzzijzzij在线观看亚洲熟妇| 久久精品亚洲精品国产色婷| 亚洲成av人片在线观看天堂无码| 四虎最新永久免费视频| 成全视频高清免费观看电视剧 | 亚洲一级片免费看| 久久亚洲国产最新网站| 亚洲一区中文字幕久久| 不卡一卡二卡三亚洲| 在线免费观看国产视频| 91麻豆最新在线人成免费观看| 免费成人在线视频观看| 中文字幕一区二区免费| 一级毛片完整版免费播放一区| 亚洲国产AV无码一区二区三区| 亚洲国产精品成人久久久|