一个类,有static变量counter,所有类实例共享

 

如果多个类实例,通过多线程访问static变量,就会产生覆盖的情况。

会发现counter偏小。

 

解决方法:

AtomicLong counter;

counter.getAndIncrement()

结果就会正确了。

 

相关文章:

  • 2022-12-23
  • 2022-02-18
  • 2021-09-21
  • 2021-12-05
  • 2021-06-27
  • 2021-09-12
  • 2022-02-10
猜你喜欢
  • 2022-12-23
  • 2021-12-21
  • 2022-12-23
  • 2021-07-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案