volatile與synchronized關鍵字

volatile關鍵字相信瞭解Java多線程的讀者都很清楚它的做用。volatile關鍵字用於聲明簡單類型變量,如int、float、boolean等數據類型。若是這些簡單數據類型聲明爲volatile,對它們的操做就會變成原子級別的。但這有必定的限制。例如,下面的例子中的n就不是原子級別的:多線程

 

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 (InterruptedException e) {
        e.printStackTrace();
       }
    }
  }

// 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 (InterruptedException e) {
//    e.printStackTrace();
//   }
//  }
// }

 public static void main(String[] args) throws InterruptedException {
      int  t = 1000;
      int  p = 0;
  while (t == 1000 && p < 1000) {
   System.out.println("n=" + JoinThread.n); 
   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);
          t = JoinThread.n;
          p++;
        JoinThread.n=0;
  }

  System.out.println("n=" + JoinThread.n);
         System.out.println("p=" + p);

 }
}

 

 

 

執行結果以下所示:spa

若是對n的操做是原子級別的,最後輸出的結果應該爲n=1000,而在執行上面代碼時,很明顯輸出的n都小於1000,這說明n=n+1不是原子級別的操做。緣由是聲明爲volatile的簡單變量若是當前值由該變量之前的值相關,那麼volatile關鍵字不起做用,也就是說以下的表達式都不是原子操做:線程

n = n + 1;
        n++;
code

 

若是要想使這種狀況變成原子操做,須要使用synchronized關鍵。將以下代碼註釋掉,並將原來註釋的代碼恢復,從新執行程序。blog

 public static volatile int n = 0;
  public void run() {
   for (int i = 0; i < 10; i++) {
    try {
     n = n + 1;
     sleep(3);// 爲了使運行結果更隨即,延遲3毫秒
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }同步

 

獲得執行結果以下:io

上面的代碼將n=n+1改爲了inc(),其中inc方法使用了synchronized關鍵字進行方法同步。所以,在使用volatile關鍵字時要慎重,並非只要簡單類型變量使用volatile修飾,對這個變量的全部操做都是原子操做,當變量的值由自身的上一個決定時,如n=n+一、n++等,volatile關鍵字將失效,只有當變量的值和自身上一個值無關時對該變量的操做纔是原子級別的,如n = m + 1,這個就是原級別的。因此在使用volatile關鍵時必定要謹慎,若是本身沒有把握,可使用synchronized來代替volatile。class

 

文章轉自:http://longshuai2007.blog.163.com/blog/static/14209441420116214435199/thread

相關文章
相關標籤/搜索