日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
初學Java多線程:慎重使用volatile關鍵字

volatile關鍵字相信了解Java多線程的讀者都很清楚它的作用。volatile關鍵字用于聲明簡單類型變量,如int、float、boolean等數(shù)據(jù)類型。如果這些簡單數(shù)據(jù)類型聲明為volatile,對它們的操作就會變成原子級別的。但這有一定的限制。例如,下面的例子中的n就不是原子級別的:

 
 
 
  1. package mythread;  
  2.  
  3. public class JoinThread extends Thread  
  4. {  
  5.     public static volatile int n = 0;  
  6.     public void run()  
  7.     {  
  8.         for (int i = 0; i < 10; i++)  
  9.             try 
  10.         {  
  11.                 n = n + 1;  
  12.                 sleep(3); // 為了使運行結果更隨機,延遲3毫秒  
  13.  
  14.             }  
  15.             catch (Exception e)  
  16.             {  
  17.             }  
  18.     }  
  19.  
  20.     public static void main(String[] args) throws Exception  
  21.     {  
  22.  
  23.         Thread threads[] = new Thread[100];  
  24.         for (int i = 0; i < threads.length; i++)  
  25.             // 建立100個線程  
  26.             threads[i] = new JoinThread();  
  27.         for (int i = 0; i < threads.length; i++)  
  28.             // 運行剛才建立的100個線程  
  29.             threads[i].start();  
  30.         for (int i = 0; i < threads.length; i++)  
  31.             // 100個線程都執(zhí)行完后繼續(xù)  
  32.             threads[i].join();  
  33.         System.out.println("n=" + JoinThread.n);  
  34.     }  
  35. }  
  36.        

如果對n的操作是原子級別的,***輸出的結果應該為n=1000,而在執(zhí)行上面積代碼時,很多時侯輸出的n都小于1000,這說明n=n+1不是原子級別的操作。原因是聲明為volatile的簡單變量如果當前值由該變量以前的值相關,那么volatile關鍵字不起作用,也就是說如下的表達式都不是原子操作:

n = n + 1;
n++;

如果要想使這種情況變成原子操作,需要使用synchronized關鍵字,如上的代碼可以改成如下的形式:

 
 
 
  1. package mythread;  
  2.  
  3. public class JoinThread extends Thread  
  4. {  
  5.     public static int n = 0;  
  6.  
  7.     public static synchronized void inc()  
  8.     {  
  9.         n++;  
  10.     }  
  11.     public void run()  
  12.     {  
  13.         for (int i = 0; i < 10; i++)  
  14.             try 
  15.             {  
  16.                 inc(); // n = n + 1 改成了 inc();  
  17.                 sleep(3); // 為了使運行結果更隨機,延遲3毫秒  
  18.  
  19.             }  
  20.             catch (Exception e)  
  21.             {  
  22.             }  
  23.     }  
  24.  
  25.     public static void main(String[] args) throws Exception  
  26.     {  
  27.  
  28.         Thread threads[] = new Thread[100];  
  29.         for (int i = 0; i < threads.length; i++)  
  30.             // 建立100個線程  
  31.             threads[i] = new JoinThread();  
  32.         for (int i = 0; i < threads.length; i++)  
  33.             // 運行剛才建立的100個線程  
  34.             threads[i].start();  
  35.         for (int i = 0; i < threads.length; i++)  
  36.             // 100個線程都執(zhí)行完后繼續(xù)  
  37.             threads[i].join();  
  38.         System.out.println("n=" + JoinThread.n);  
  39.     }  
  40. }  

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

【編輯推薦】

  1. 初學Java多線程:join方法的使用
  2. 初學Java多線程:線程的生命周期
  3. 初學Java多線程:使用Runnable接口創(chuàng)建線程
  4. 初學Java多線程:用Thread類創(chuàng)建線程
  5. 初學Java多線程:線程簡介

當前標題:初學Java多線程:慎重使用volatile關鍵字
地址分享:http://www.5511xx.com/article/dpgodgj.html