java的int类型线程安全

本文转载自:http://blog.csdn.net/xiaoyezi1001/article/details/25504935

一、测试int为线程不安全

Java中,高并发/多线程情况下,int的自增自减操作都不是线程安全的,使用AtomicInteger可以保证。

[java]  view plain  copy
  1. package com.yezi.learn.atomic;  
  2. import java.util.concurrent.atomic.AtomicInteger;  
  3. /** 
  4.  * Created by yezi on 2014/5/10. 
  5.  */  
  6. public class TestAtomicInteger {  
  7.     private int shareI=0;  
  8.     private AtomicInteger atomicLoop = new AtomicInteger(0);  
  9.     private int loop =0;  
  10.     public static void main(String []args) throws Exception{  
  11.         TestAtomicInteger t = new TestAtomicInteger();  
  12.         t.testMethod();  
  13.         System.out.println(t);  
  14.     }  
  15.     public void testMethod() throws Exception{  
  16.         Thread th1 = new Thread(new ThreadTest());  
  17.         Thread th2 = new Thread(new ThreadTest());  
  18.         th1.start();  
  19.         th2.start();  
  20.         th1.join();  
  21.         th2.join();  
  22.     }  
  23.     public synchronized void add(){  
  24.         shareI++;  
  25.     }  
  26.     class ThreadTest implements Runnable{  
  27.         @Override  
  28.         public void run() {  
  29.             //testAtomicInteger();  
  30.             testInt();  
  31.         }  
  32.     }  
  33.     public void testInt(){ //测试,输出结果随机,为线程不安全  
  34.         for(;loop<100000;loop++) {  
  35.             add();  
  36.         }  
  37.     }  
  38.     public void testAtomicInteger(){  //输出1000000,线程安全  
  39.         for(;atomicLoop.getAndAdd(1)<100000;) {  
  40.             add();  
  41.         }  
  42.     }  
  43.     @Override  
  44.     public String toString() {  
  45.         return ""+shareI;  
  46.     }  
  47. }  

二、AtomicIntegerFieldUpdater可以对Obj的字段进行处理

[java]  view plain  copy
  1. package com.yezi.learn.atomic;  
  2. import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;  
  3. /** 
  4.  * Created by yezi on 2014/5/10. 
  5.  */  
  6. public class TestAtomicObjectInteger {  
  7.     private Looper looper = new Looper();  
  8.     private int shareI=0;  
  9.     public static void main(String ...args) throws Exception{  
  10.         TestAtomicObjectInteger t = new TestAtomicObjectInteger();  
  11.         t.testMethod();  
  12.         System.out.println(t);  
  13.     }  
  14.     public void testNormal(){ //线程不安全的  
  15.         for(;looper.getLoop()<100000;looper.setLoop(looper.getLoop()+1)){  
  16.             add();  
  17.         }  
  18.     }  
  19.     public void testUseAtomic(){  
  20.         AtomicIntegerFieldUpdater<Looper> atomicIntegerFieldUpdater =  //使用Atomic来进行自加  
  21.                 AtomicIntegerFieldUpdater.newUpdater(Looper.class,"loop");  
  22.         for(;atomicIntegerFieldUpdater.getAndAdd(looper,1)<100000;){  
  23.             add();  
  24.         }  
  25.     }  
  26.     public void testMethod() throws Exception{  
  27.         Thread th1 = new Thread(new ThreadTest());  
  28.         Thread th2 = new Thread(new ThreadTest());  
  29.         th1.start();  
  30.         th2.start();  
  31.         th1.join();  
  32.         th2.join();  
  33.     }  
  34.     class ThreadTest implements Runnable{  
  35.         @Override  
  36.         public void run() {  
  37.             testUseAtomic();  
  38.             //testNormal();  
  39.         }  
  40.     }  
  41.     public synchronized void add(){  
  42.         shareI++;  
  43.     }  
  44.     @Override  
  45.     public String toString() {  
  46.         return shareI+"";  
  47.     }  
  48. }  
  49. class Looper{  
  50.     public volatile int loop=0;  //对于AtomicIntegerFieldUpdater要控制的字段必须为public volatile  
  51.     public int getLoop() {  
  52.         return loop;  
  53.     }  
  54.     public void setLoop(int loop) {  
  55.         this.loop = loop;  
  56.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值