一般如果我们自己写一个计数器方法,需要考虑线程安全问题,尤其高并发访问的时候。

AtomicLong 已处理并发问题,直接使用。java.util.concurrent.atomic包提供多种线程安全计数器

import java.util.concurrent.atomic.AtomicLong;  
  
  
public class Counter {  
    private static AtomicLong counter = new AtomicLong(0);  
  
  
    public static long addOne() {  
        return counter.incrementAndGet();  
    }  
} 

详情参考:http://blog.csdn.net/yaqingwa/article/details/17737771

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-29
  • 2021-12-21
  • 2021-07-12
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-22
  • 2022-12-23
  • 2022-02-24
  • 2021-05-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案