volatile的不保证原子性

 1 package volati;
 2 
 3 /**
 4  * Created by lhw on 16-7-29.
 5  */
 6 public class Demo {
 7     private static volatile long longVal = 0;
 8 
 9 
10     public static void main(String[] args) {
11 
12         new Thread(new Runnable() {
13             @Override
14             public void run() {
15                 for (int i =0 ; i<10 ;i ++) {
16                     longVal = longVal + 1;
17                     System.out.println(longVal);
18                 }
19             }
20         }).start();
21 
22 
23         new Thread(
24                 new Runnable(){
25                     @Override
26                     public void run() {
27                         longVal =10;
28                         System.out.println(longVal+"haha");
29                     }
30                 }).start();
31         
32     }
33     
34 }

result

posted on 2018-01-14 17:07  剑姬  阅读(252)  评论(0编辑  收藏  举报

导航