volatile关键字相信了解Java多线程的读者都很清楚它的做用。volatile关键字用于声明简单类型变量,如int、float、 boolean等数据类型。若是这些简单数据类型声明为volatile,对它们的操做就会变成原子级别的。但这有必定的限制。例如,下面的例子中的n就 不是原子级别的:
Java代码
- package mythread;
-
- public class JoinThread extends Thread
- {
- public static volatile int n = 0 ;
- public void run()
- {
- for ( int i = 0 ; i < 10 ; i ++ )
- try
- {
- n = n + 1 ;
- sleep( 3 ); // 为了使运行结果更随机,延迟3毫秒
-
- }
- catch (Exception e)
- {
- }
- }
-
- public static void main(String[] args) throws Exception
- {
-
- Thread threads[] = new Thread[ 100 ];
- for ( int i = 0 ; i < threads.length; i ++ )
- // 创建100个线程
- threads[i] = new JoinThread();
- for ( int i = 0 ; i < threads.length; i ++ )
- // 运行刚才创建的100个线程
- threads[i].start();
- for ( int i = 0 ; i < threads.length; i ++ )
- // 100个线程都执行完后继续
- threads[i].join();
- System.out.println( " n= " + JoinThread.n);
- }
- }
若是对n的操做是原子级别的,最后输出的结果应该为n=1000,而在执行上面积代码时,不少时侯输出的n都小于1000,这说明n=n+1不是原子级别 的操做。缘由是声明为volatile的简单变量若是当前值由该变量之前的值相关,那么volatile关键字不起做用,也就是说以下的表达式都不是原子 操做:
n = n + 1 ;
n ++ ;
若是要想使这种状况变成原子操做,须要使用synchronized关键字,如上的代码能够改为以下的形式:
Java代码
- package mythread;
-
- public class JoinThread extends Thread
- {
- public static int n = 0 ;
-
- public static synchronized void inc()
- {
- n ++ ;
- }
- public void run()
- {
- for ( int i = 0 ; i < 10 ; i ++ )
- try
- {
- inc(); // n = n + 1 改为了 inc();
- sleep( 3 ); // 为了使运行结果更随机,延迟3毫秒
-
- }
- catch (Exception e)
- {
- }
- }
-
- public static void main(String[] args) throws Exception
- {
-
- Thread threads[] = new Thread[ 100 ];
- for ( int i = 0 ; i < threads.length; i ++ )
- // 创建100个线程
- threads[i] = new JoinThread();
- for ( int i = 0 ; i < threads.length; i ++ )
- // 运行刚才创建的100个线程
- threads[i].start();
- for ( int i = 0 ; i < threads.length; i ++ )
- // 100个线程都执行完后继续
- threads[i].join();
- System.out.println( " n= " + JoinThread.n);
- }
- }
上面的代码将n=n+1改为了inc(),其中inc方法使用了synchronized关键字进行方法同步。所以,在使用volatile关键字时要慎 重,并非只要简单类型变量使用volatile修饰,对这个变量的全部操做都是原来操做,当变量的值由自身的上一个决定时,如n=n+一、n++ 等,volatile关键字将失效,只有当变量的值和自身上一个值无关时对该变量的操做才是原子级别的,如n = m + 1,这个就是原级别的。因此在使用volatile关键时必定要谨慎,若是本身没有把握,可使用synchronized来代替volatile。