中文国产日韩欧美视频,午夜精品999,色综合天天综合网国产成人网,色综合视频一区二区观看,国产高清在线精品,伊人色播,色综合久久天天综合观看

threadLocal 原理與使用 -電腦資料

電腦資料 時間:2019-01-01 我要投稿
【www.szmdbiao.com - 電腦資料】

   

目標:用于每個線程資源的隔離.

    當工作于多線程中的對象使用ThreadLocal 維護變量時,ThreadLocal 為 每個使用該變量的線程分配一個獨立的變量副本,

threadLocal 原理與使用

。每個線程獨立改變自己的副本,而不影響其他線程所對應的變量副本。

    不同于線程同步:線程同步用于解決一個變量同一時刻被多個線程共享的問題(共享一個變量)。

    threadLocal 使得一個變量在多個線程中各自擁有自己的副本(實現(xiàn)資源的隔離)。

原理:

    ThreadLocal 中有一個map, 用于存儲每一個線程的變量副本,其中map的key為 線程對象,value為對應線程的變量副本。

<code class="hljs cs">public class SimpleThreadLocal<t>{    private Map valueMap = Collections.synchronizedMap(new HashMap());    public void set(T value) {        valueMap.put(Thread.currentThread(), value);    }    public T get() {        Thread currentThread = Thread.currentThread();        T value = (T) valueMap.get(currentThread);        if(value == null && !valueMap.containsKey(currentThread)) {            value = initValue();            valueMap.put(currentThread, value);        }        return value;    }    public void remove() {        valueMap.remove(Thread.currentThread());    }    public T initValue() {        return null;    }}</t></code>

應用場景:

    web應用分層結(jié)構(gòu)中:同一變量在持久化,業(yè)務(wù),展示層傳遞時(同一線程)。

接口方法:

<code class="hljs cs">protected T initialValue(); //return the initial value for this thread-local    public T get(); //return the current thread's value of this thread-local    public void set(T value); //@param value the value to be stored in the current thread's copy of this thread-local.    public void remove(); //Removes the current thread's value for this thread-local variable.</code>

示例:

<code class="hljs cs">ThreadLocalContext.classpublic class ThreadLocalContext {    private static ThreadLocal<integer>threadLocalNum = new ThreadLocal<integer>() {        @Override        protected Integer initialValue() {            return 0;        }    };    public int getNextNum() {        threadLocalNum.set(threadLocalNum.get()+1);        return threadLocalNum.get();    }}</integer></integer></code>
<code class="hljs axapta">ThreadLocalTest.classpublic class ThreadLocalTest {    public static void main(String[] args) {        ThreadLocalContext threadLocalContext  = new ThreadLocalContext();        WorkThread workThread1 = new WorkThread(threadLocalContext);        WorkThread workThread2 = new WorkThread(threadLocalContext);        WorkThread workThread3 = new WorkThread(threadLocalContext);        workThread1.start();        workThread2.start();        workThread3.start();    }}class WorkThread extends Thread{    private ThreadLocalContext threadLocalContext;    public WorkThread(ThreadLocalContext threadLocalContext) {        this.threadLocalContext = threadLocalContext;    }    public void run() {        for(int i = 0; i < 5; i++) {            System.out.println("thread[" + Thread.currentThread().getName()                    + "] threadLocalNum [" + threadLocalContext.getNextNum() + "]");        }    }}</code>

最新文章