博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于Java ThreadLocal
阅读量:6251 次
发布时间:2019-06-22

本文共 6410 字,大约阅读时间需要 21 分钟。

hot3.png

转自:

What is ThreadLocal? A simple example

As its name suggests, a single instance of ThreadLocal can store different values for each thread independently. Therefore, the value stored in a ThreadLocal instance is specific (local) to the current running Thread, any other code logic running on the same thread will see the same value, but not the values set on the same instance by other threads. (There are exceptions, like InhertiableThreadLocal, which inherits parent thread’s values by default.)

Let’s consider this example:

We have a TransactionManager class that provide static methods to:

  • Start a transaction with a generated ID

  • Store that ID as a static field and provide a transaction ID getter method to other code logic that needs to know the current transaction ID.

In a single threaded environment, TransactionManager can simply store the ID as a static field and return as is. However, this will certainly not work in a multiple-threaded environment. Imagine multiple threads are using TransactionManager – transaction IDs generated by each thread can overwrite each other as there is only one static instance of transaction ID. One may synchronize and block other transactions to avoid overwrites, but this would totally defeat the purpose of having multiple threads.

In order to solve this problem, ThreadLocal provides a very neat solution:

public class TransactionManager {    private static final ThreadLocal
 context = new ThreadLocal
();    public static void startTransaction() {        //logic to start a transaction        //...        context.set(generatedId);    }    public static String getTransactionId() {        return context.get();    }    public static void endTransaction() {        //logic to end a transaction        //…        context.remove();    }}

Different thread that starts transactions via TransactionManager will get its own transaction ID stored in the context. Any logic within the same thread can call getTransactionId() later on to retrieve the value belongs/local to that Thread. So problem’s solved!

The Internals of ThreadLocal and How it Works

Let’s drill down a little bit into the ThreadLocal’s internals. ThreadLocal is implemented by having a Map (a ThreadLocalMap) as field (with  entry) within each Thread instance. (There are actually 2 maps; the second one is used for InheritabeleThreadLocal, but let’s not complicate the picture). The keys of those maps are the corresponding ThreadLocals themselves. Therefore, when a set/get is called on a ThreadLocal, it looks at the current thread, find the map, and look up the value with “this” ThreadLocal instance.

Still confused? I certainly am. Let’s look at a real example.

  • Code running in Thread 1 calls set() on ThreadLocal instance “A” with value “123″

  • Code running in Thread 2 calls set() on ThreadLocal instance “A” with value “234″

  • Code running in Thread 1 calls set() on ThreadLocal instance “B” with value “345″

And this is the end result:

Thread 1 (the instance)’s field ThreadLocalMap (m1) has 2 entries:

key value
ThreadA “123”
ThreadB “345”
Thread 2 (the instance)’s field ThreadLocalMap (m2) has 1 entry:

key value
ThreadB “234”

Now if some code logic in Thread 1 calls get() on ThreadLocal instance “A”, the ThreadLocal logic will lookup the current Thread, which is instance Thread 1, then access the field ThreadLocalMap of that Thread instance, which is m1, it can then lookup the value by using m1.get(this), with “this” as ThreadLocal and the result is “123″

Now what to watch out for!

Did I hear weak reference for ThreadLocal entries? Does that mean I don’t have to clean up? Well, it’s not quite that simple.

First of all, the value object put into the ThreadLocal would not purge itself (garbage collected) if there are no more Strong references to it. Instead, the Weak reference is done on the thread instance, which means Java garbage collection would clean up the ThreadLocal map if the thread itself is not strongly referenced elsewhere.

So now the question is: when would the Thread object get garbage collected?

The answer is: it depends, but always assume the thread is long running. 2 common examples:

  • Servlets. The threads that handle servlet requests usually stay alive in the container for the lifetime of the server instance. Code logic that uses ThreadLocal might be referenced indirectly by servlets.

  • Thread pooling java.util.concurrent.Executors. Java encourages recycling threads!

A typical usage of Executor introduced in Java 1.5, if ThreadLocal maps are not cleaned up properly after a transaction is done, next TransactionProcessingTask might inherit values from another previous unrelated task!

ExecutorService service = Executors.newFixedThreadPool(10);service.submit(new TransactionProcessingTask());

Be careful with initialization of ThreadLocal, below is an implementation of a counter by Thread. Can you tell what is wrong in the below initialization?

public class Counter {    private static ThreadLocal
 counter =        new ThreadLocal
();    static {        counter.set(0);    }    public int getCountInThread() {        return counter.get();    }    //…}

The counter would not get initialized correctly! Though the counter is declared as static, it CANNOT be initialized by having a static initializer, as the initializer only runs once when the first thread references the Counter class. When the second thread comes in, it does not run counter.set(0) on that thread, therefore counter.get() returns null instead of 0! One solution is to sublcass ThreadLocal and override the initialValue() method to assign non-null initial value.

With these in mind, you can probably picture the consequences of not cleaning up after ourselves! An operation that runs on a recycled thread might inherit the values from previous operation on the same thread! Besides, it can also cause memory leaks as the instance stored in ThreadLocal will never get garbage collected if the thread is alive.

As a rule of thumb, always clean up/reset your threadlocal after you have finished your “unit of operation”! Even though the current code might be simple enough to bypass the cleanups, it might be adapted and integrated into servlets/thread pooling later on! After all, cleaning up responsibly is always appreciated both in the realms of programming and real life.

转载于:https://my.oschina.net/u/552375/blog/302467

你可能感兴趣的文章
win7读取linux硬盘序列号,Windows 下获取硬盘序列号
查看>>
linux音频设备接口,OSS--跨平台的音频接口简介
查看>>
华为网卡linux驱动安装,Linux Nvidia显卡驱动安装
查看>>
linux sql撤销,取消请求的sql语句
查看>>
c语言学习 二维指针,二维数组和指针(C语言)
查看>>
图像压缩算法构造最优解c语言,C语言与程序设计第12章递归.ppt
查看>>
c语言飞机源代码,C语言写的飞机源码
查看>>
C语言 如果某个数大于10 归零,C:当指针实际指向某个东西时,函数继续接收归零指针(示例代码)...
查看>>
c c 语言项目实战 pdf,[计算机]C实战项目.pdf
查看>>
linux中solr创建core,Solr6.6 创建core
查看>>
android的边框阴影,android 自定义shape 带阴影边框效果
查看>>
android centos 的编码,Centos 安装 android sdk
查看>>
反编译android 状态栏沉浸,手把手教你傻瓜式开启状态栏沉浸模式
查看>>
android l job scheduler api,Android JobScheduler API
查看>>
html css 扑克牌桌面,纯CSS实现画扑克牌
查看>>
html5资源分享,12款实用的HTML5干货分享
查看>>
n9 android模拟器,Android软件将兼容诺基亚N9
查看>>
html防替换资源,html前端进行资源重载及刷新资源
查看>>
html5css做星星的形状,css评分效果的星星示例技术分享
查看>>
html实现放大镜效果,利用jquery实现放大镜特效
查看>>