今天看了下 Java中的同步机制,刚刚开始看,内容比较浅,其中有一个被称为原子变量类的东西感觉还是很有意思的,所以,记录一下吧。
首先,我们先看一段这样的代码:
public class AtomicClassTest implements Runnable{ /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } @Override public void run() { // TODO Auto-generated method stub while (true) { ++Main.i; System.out.println(Thread.currentThread().getName() + " : " + Main.i); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
public class Main { public static int i = 0; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Thread thread1 = new Thread(new AtomicClassTest()); Thread thread2 = new Thread(new AtomicClassTest()); thread1.start(); thread2.start(); } }
Thread-0 : 2 Thread-1 : 2 Thread-1 : 3 Thread-0 : 3 Thread-0 : 5 Thread-1 : 5 Thread-0 : 6 Thread-1 : 7 Thread-0 : 8 Thread-1 : 9 Thread-0 : 10
从结果中我们看到,出现了不同步的问题。这里,我们需要知道一个事实,变量++ 操作并不是原子的,它实际上是分了三步完成的,首先,从内存中读取变量值放入到寄存器中,之后,执行 +1 操作,最后,把变量放入到内存中。所以,存在多个线程竞争执行的状态。
在java中,存在原子变量的类,它定义起来就像是下面这样:
AtomicInteger iAtomicInteger = new AtomicInteger(0);
好吧,重新写下上面的代码:
public class __AtomicClass implements Runnable{ /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } @Override public void run() { // TODO Auto-generated method stub while (true) { System.out.println(Thread.currentThread().getName() + " : " + AtomicMain.iAtomicInteger.incrementAndGet()); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
public class AtomicMain { //原子变量类 public static AtomicInteger iAtomicInteger = new AtomicInteger(0); /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Thread thread1 = new Thread(new __AtomicClass()); Thread thread2 = new Thread(new __AtomicClass()); thread1.start(); thread2.start(); } }验证下这个类的作用吧:
Thread-1 : 1 Thread-0 : 2 Thread-1 : 3 Thread-0 : 4 Thread-1 : 5 Thread-0 : 6 Thread-1 : 7 Thread-0 : 8 Thread-1 : 9 Thread-0 : 10 Thread-1 : 11 Thread-0 : 12 Thread-0 : 14 Thread-1 : 13 Thread-1 : 15
原文地址:http://blog.csdn.net/dlutbrucezhang/article/details/41350613