ThreadLocal< AtomicInteger>可能有用吗? [英] Could ThreadLocal<AtomicInteger> possibly be useful?

查看:240
本文介绍了ThreadLocal< AtomicInteger>可能有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我只是看到有人尝试使用 c0>在某些Java代码中.
现在,对于链接的代码而言,除了导致请求被拒绝的其他问题之外,这显然毫无用处.

So I just saw someone try to use a ThreadLocal<AtomicInteger> in some Java code.
Now, for the linked code, that's clearly useless, among other problems which caused the request to be denied.

似乎总是无用的:AtomicInteger(来自java.util.concurrent.atomic包)专为多线程访问而设计,而ThreadLocal使得每个线程都有自己的值,所以为什么还要使用它吗?

And it seems like it would always be useless: AtomicInteger (from the java.util.concurrent.atomic package) is designed for multithread access, and ThreadLocal makes each thread have its own value, so why even use it?

我的问题是:在任何情况下ThreadLocal<AtomicInteger>都会有用吗?

My question is: Is there any situation in which a ThreadLocal<AtomicInteger> would be useful?

推荐答案

是的,我们可能会提出一个合理的方案:

Yes, we may come up with a legitimate scenario:

  1. 在每个任务的开始我们需要AtomicInteger的线程本地实例;
  2. 我们继续在其他几个线程之间分配该对象,例如,主任务线程派生的子线程.
  1. we need a thread-local instance of AtomicInteger at the start of each task;
  2. we proceed to distribute this object among several other threads, for example child threads forked by the main task thread.

如果不评估出现这种情况的整体情况,我们将无法判断.

Without assessing the totality of the context where this appears, we cannot judge.

这篇关于ThreadLocal&lt; AtomicInteger&gt;可能有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆