Android——ThreadLocal

一. ThreadLocal介绍

ThreadLocal可以在不同的线程中互不干扰的存储数据。
ThreadLocal是一个线程内部的数据存储类,通过它可以在指定的线程中存储数据,数据存储后,只有在指定线程中可以获取到存储的数据,对于其他线程来说则无法取到数据。

二. 例子

public class ThreadLocalActivity extends AppCompatActivity {

    private ThreadLocal threadLocal;
    private static final String TAG = "ThreadLocalActivity-->";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread_local);

        threadLocal = new ThreadLocal();
        threadLocal.set("初始值 0");
        Log.d(TAG, "当前线程:" + Thread.currentThread().getName() + "  threadLocal值为:" + threadLocal.get());

        HandlerThread handlerThread1 = new HandlerThread("线程一");
        HandlerThread handlerThread2 = new HandlerThread("线程二");
        handlerThread1.start();
        handlerThread2.start();

        Handler handler1 = new Handler(handlerThread1.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                threadLocal.set("子线程1设置值为 1");
                Log.d(TAG, "当前线程:" + Thread.currentThread().getName() + "  threadLocal值为:" + threadLocal.get());
            }
        };
        Handler handler2 = new Handler(handlerThread2.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.d(TAG, "当前线程:" + Thread.currentThread().getName() + "  threadLocal值为:" + threadLocal.get());
            }
        };

        handler1.sendEmptyMessage(1);
        handler2.sendEmptyMessage(2);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "当前线程:" + Thread.currentThread().getName() + "  threadLocal值为:" + threadLocal.get());
    }

}

运行该ThreadLocalActivity ,得到的日志为:

04-27 15:02:01.283 14375-14375/pri.hsj.thread D/ThreadLocalActivity-->: 当前线程:main  threadLocal值为:初始值 0
04-27 15:02:01.284 14375-14440/pri.hsj.thread D/ThreadLocalActivity-->: 当前线程:线程二  threadLocal值为:null
04-27 15:02:01.284 14375-14439/pri.hsj.thread D/ThreadLocalActivity-->: 当前线程:线程一  threadLocal值为:子线程1设置值为 1
04-27 15:02:01.784 14375-14375/pri.hsj.thread D/ThreadLocalActivity-->: 当前线程:main  threadLocal值为:初始值 0

三. 原因分析和总结

我们看下ThreadLocal源码:ThreadLocal#set()方法和ThreadLocal#get()方法

    public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }
    public T get() {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null) {
                @SuppressWarnings("unchecked")
                T result = (T)e.value;
                return result;
            }
        }
        return setInitialValue();
    }

我们会发现不管是set()方法还是get()方法,都是对当前线程做操作,因此在不同线程中访问同一个ThreadLocal的set()方法和get()方法,它们对ThreadLocal所作的读/写操作仅限于各自线程的内部,这就是为什么ThreadLocal可以在多个线程中互不干扰的存储和修改数据。

你可能感兴趣的:(Android——ThreadLocal)