线程安全的计数器

不能通过编译的例子。

Creature creature = new Creature();是一个局部变量声明语句(local variable declaration statement) JLS 4.4,java语言规范不允许一个局部变量声明语句作为一条语句在循环中重复执行。一个本地变量声明作为一条只能直接出现在一个语句块中。(一个语句块又括号以及其中包括的语句和声明构成)

 

Java代码    收藏代码
  1. package arkblue.lang.javapuzzler.n55;  
  2.   
  3. public class Creator {  
  4.     public static void main(String[] args) {  
  5.     for (int i = 0; i < 100; i++)  
  6.         // 不能通过编译  
  7.     Creature creature = new Creature();  
  8.     System.out.println(Creature.numCreated());  
  9.     }  
  10. }  
  11.   
  12. class Creature {  
  13.     private static long numCreated = 0;  
  14.   
  15.     public Creature() {  
  16.         numCreated++;  
  17.     }  
  18.   
  19.     public static long numCreated() {  
  20.         return numCreated;  
  21.     }  
  22. }  

 

 

修改后:

  

Java代码    收藏代码
  1. package arkblue.lang.javapuzzler.n55;  
  2.   
  3. public class Creator {  
  4.     public static void main(String[] args) {  
  5.     <span style="color: #ff0000;">for (int i = 0; i < 100; i++) {  
  6.         Creature creature = new Creature();  
  7.     }</span>  
  8.               
  9.     System.out.println(Creature.numCreated());  
  10.     }  
  11. }  
  12.   
  13. class Creature {  
  14.     private static long numCreated = 0;  
  15.   
  16.     public Creature() {  
  17.         numCreated++;  
  18.     }  
  19.   
  20.     public static long numCreated() {  
  21.         return numCreated;  
  22.     }  
  23. }  

 

但是上面的代码不是线程安全的,使用synchronized关键字后的效果:

 

Java代码    收藏代码
  1. package arkblue.lang.javapuzzler.n55;  
  2.   
  3. public class Creator {  
  4.     public static void main(String[] args) {  
  5.     for (int i = 0; i < 100; i++) {  
  6.         Creature creature = new Creature();  
  7.     }  
  8.               
  9.     System.out.println(Creature.numCreated());  
  10.     }  
  11. }  
  12.   
  13. class Creature {  
  14.     private static long numCreated;  
  15.         public Creature() {  
  16.             <span style="color: #ff0000;">synchronized (Creature.class) {  
  17.                 numCreated++;  
  18.             }</span>  
  19.         }  
  20.           
  21.     public static <span style="color: #ff0000;">synchronized</span> long numCreated() {  
  22.         return numCreated;  
  23.     }  
  24. }  

 

 

在5.0后,可以使用AtomicLong实例,他在面临并发的时候绕过对同步的需求。

 

Java代码    收藏代码
  1. package arkblue.lang.javapuzzler.n55;  
  2.   
  3. import java.util.concurrent.atomic.AtomicLong;  
  4.   
  5. public class Creator {  
  6.     public static void main(String[] args) {  
  7.     for (int i = 0; i < 100; i++) {  
  8.         @SuppressWarnings("unused")  
  9.         Creature creature = new Creature();  
  10.     }  
  11.               
  12.     System.out.println(Creature.numCreated());  
  13.     }  
  14. }  
  15.   
  16. class Creature {  
  17.     private static AtomicLong numCreated = new AtomicLong();  
  18.     public Creature() {  
  19.     numCreated.incrementAndGet();  
  20.     }  
  21.     public static long numCreated() {  
  22.     return numCreated.get();  
  23.     }  
  24. }  

 

结论

(1)一个本地变量声明语句不能用在循环中的重复执行语句,他作为一条语句只能出现在语句块中。

(2)使用变量作为计数器,要使用long,而不是int,以防止溢出。

(3)在多线程环境下,要么对计数器同步,要么使用AutomicLong。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值