volatile關鍵字相信了解Java多線程的讀者都很清楚它的作用。volatile關鍵字用于聲明簡單類型變量,如int、float、boolean等數(shù)據類型。如果這些簡單數(shù)據類型聲明為volatile,對它們的操作就會變成原子級別的。但這有一定的限制。
例如,下面的例子中的n就不是原子級別的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關鍵字,如上的代碼可以改成如下的形式
37.package mythread;
38.
39.public class JoinThread extends Thread
40.{
41. public static int n = 0;
42.
43. public static synchronized void inc()
44. {
45. n++;
46. }
47. public void run()
48. {
49. for (int i = 0; i < 10; i++)
50. try
51. {
52. inc(); // n = n + 1 改成了 inc();
53. sleep(3); // 為了使運行結果更隨機,延遲3毫秒
54.
55. }
56. catch (Exception e)
57. {
58. }
59. }
60.
61. public static void main(String[] args) throws Exception
62. {
63.
64. Thread threads[] = new Thread[100];
65. for (int i = 0; i < threads.length; i++)
66. // 建立100個線程
67. threads[i] = new JoinThread();
68. for (int i = 0; i < threads.length; i++)
69. // 運行剛才建立的100個線程
70. threads[i].start();
71. for (int i = 0; i < threads.length; i++)
72. // 100個線程都執(zhí)行完后繼續(xù)
73. threads[i].join();
74. System.out.println("n=" + JoinThread.n);
75. }
76.}
上面的代碼將n=n+1改成了inc(),其中inc方法使用了synchronized關鍵字進行方法同步。因此,在使用volatile關鍵字時要慎重,并不是只要簡單類型變量使用volatile修飾,對這個變量的所有操作都是原來操作,當變量的值由自身的上一個決定時,如n=n+1、n++等,volatile關鍵字將失效,只有當變量的值和自身上一個值無關時對該變量的操作才是原子級別的,如n = m + 1,這個就是原級別的。所以在使用volatile關鍵時一定要謹慎,如果自己沒有把握,可以使用synchronized來代替volatile。
例如,下面的例子中的n就不是原子級別的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關鍵字,如上的代碼可以改成如下的形式
37.package mythread;
38.
39.public class JoinThread extends Thread
40.{
41. public static int n = 0;
42.
43. public static synchronized void inc()
44. {
45. n++;
46. }
47. public void run()
48. {
49. for (int i = 0; i < 10; i++)
50. try
51. {
52. inc(); // n = n + 1 改成了 inc();
53. sleep(3); // 為了使運行結果更隨機,延遲3毫秒
54.
55. }
56. catch (Exception e)
57. {
58. }
59. }
60.
61. public static void main(String[] args) throws Exception
62. {
63.
64. Thread threads[] = new Thread[100];
65. for (int i = 0; i < threads.length; i++)
66. // 建立100個線程
67. threads[i] = new JoinThread();
68. for (int i = 0; i < threads.length; i++)
69. // 運行剛才建立的100個線程
70. threads[i].start();
71. for (int i = 0; i < threads.length; i++)
72. // 100個線程都執(zhí)行完后繼續(xù)
73. threads[i].join();
74. System.out.println("n=" + JoinThread.n);
75. }
76.}
上面的代碼將n=n+1改成了inc(),其中inc方法使用了synchronized關鍵字進行方法同步。因此,在使用volatile關鍵字時要慎重,并不是只要簡單類型變量使用volatile修飾,對這個變量的所有操作都是原來操作,當變量的值由自身的上一個決定時,如n=n+1、n++等,volatile關鍵字將失效,只有當變量的值和自身上一個值無關時對該變量的操作才是原子級別的,如n = m + 1,這個就是原級別的。所以在使用volatile關鍵時一定要謹慎,如果自己沒有把握,可以使用synchronized來代替volatile。