Java – could ThreadLocal be useful?

So I just saw someone try to use a ThreadLocal < atomicinteger > in some java code

And it always seems useless: atomicinteger (from the Java. Util. Concurrent. Atomic package) is designed for multi-threaded access, and ThreadLocal makes each thread have its own value, so why even use it?

My question is: is ThreadLocal < atomicinteger > useful in any case

Solution

Yes, we may come up with a legal situation:

>At the beginning of each task, we need a thread local atomicinteger instance; > We continue to distribute this object among other threads, such as child threads forked by the main task thread

We cannot judge without evaluating the overall situation of this situation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>