2.3.6原子类也并不完全安全
原子类在具有逻辑的情况下输出结果也具有随机性
package com.cky.thread; import java.util.concurrent.atomic.AtomicLong; /** * Created by edison on 2017/12/9. */ public class MyService { public static AtomicLong ai= new AtomicLong(); public void addNum() { System.out.println(Thread.currentThread().getName() +" 加了100之后的值是"+ ai.getAndAdd(100)); ai.getAndAdd(1); } }
package com.cky.thread; /** * Created by edison on 2017/12/9. */ public class MyThread extends Thread{ private MyService service; public MyThread(MyService service) { this.service =service; } @Override public void run() { super.run(); service.addNum(); } }
package com.cky.test; import com.cky.thread.MyService; import com.cky.thread.MyThread; /** * Created by edison on 2017/12/9. */ public class Run { public static void main(String[] args) { try { MyService myService = new MyService(); MyThread[] myThreads = new MyThread[5]; for (int i = 0; i < myThreads.length; i++) { myThreads[i] = new MyThread(myService); } for (int i = 0; i < myThreads.length; i++) { myThreads[i].start(); } Thread.sleep(1000); System.out.println(myService.ai.get()); } catch (InterruptedException e) { e.printStackTrace(); } } }
Thread-1 加了100之后的值是0 Thread-4 加了100之后的值是400 Thread-2 加了100之后的值是300 Thread-3 加了100之后的值是200 Thread-0 加了100之后的值是100 505
结果分析:
打印顺序出错了,应该每次加100在加一次1,出现这样的情况原因是addAndGet()方法是原子的,但方法和方法直接的调用却不是原子的
解决方案同步
package com.cky.thread; import java.util.concurrent.atomic.AtomicLong; /** * Created by edison on 2017/12/9. */ public class MyService { public static AtomicLong ai= new AtomicLong(); synchronized public void addNum() { System.out.println(Thread.currentThread().getName() +" 加了100之后的值是"+ ai.getAndAdd(100)); ai.getAndAdd(1); } }
Thread-1 加了100之后的值是0 Thread-3 加了100之后的值是101 Thread-4 加了100之后的值是202 Thread-0 加了100之后的值是303 Thread-2 加了100之后的值是404 505
相关推荐
AlexZhao 2020-06-04
gongruitao 2019-12-27
ciscoccie00 2019-12-19
lvxingzhe 2019-11-04
xienue 2019-10-29
wsxsxz 2019-10-21
书山有路勤为径 2019-07-01
guchengxinfen 2017-06-07
Jhonse技术 2011-01-07
山水沐光 2019-05-09
Broadview 2010-02-19
Franklyn 2011-12-03
朝气蓬勃 2019-06-12
sgafdsg 2011-04-18
hello小工 2017-10-09
VacantYang 2018-04-05
89284052 2016-10-15