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