今天突然理解了“volatile”

來源:互聯網
上載者:User

首先咱們可以看一下MSDN對volatile的定義:
The volatile keyword indicates that a field can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.

The system always reads the current value of a volatile object at the point it is requested, even if the previous instruction asked for a value from the same object. Also, the value of the object is written immediately on assignment.

The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access. Using the volatile modifier ensures that one thread retrieves the most up-to-date value written by another thread.

然後呢,咱們可以看到在Singleton模式中對多線程的最佳化

 1using System;
 2using System.Configuration;
 3
 4namespace HeadFirstDesignPatterns.Singleton.InterestRate
 5{
 6    /**//// <summary>
 7    /// Summary description for RateSingleton.
 8    /// </summary>
 9    public class RateSingleton
10    {
11        private volatile static RateSingleton uniqueInstance;
12        private static object syncRoot = new Object();
13
14        private double currentRate = 
15            Convert.ToDouble(ConfigurationSettings.AppSettings["CurrentInterestRate"]);
16
17        public double CurrentRate
18        {
19            get
20            {
21                return currentRate;
22            }
23            set
24            {
25                currentRate = value;
26            }
27        }
28
29        private RateSingleton()
30        {}
31
32        public static RateSingleton GetInstance()
33        {
34            //The approach below ensures that only one instance is created and only 
35            //when the instance is needed. Also, the uniqueInstance variable is 
36            //declared to be volatile to ensure that assignment to the instance variable 
37            //completes before the instance variable can be accessed. Lastly, 
38            //this approach uses a syncRoot instance to lock on, rather than 
39            //locking on the type itself, to avoid deadlocks.
40
41            if(uniqueInstance == null)
42            {
43                lock (syncRoot)
44                {
45                    if(uniqueInstance == null)
46                    {
47                        uniqueInstance = new RateSingleton();
48                    }
49                }
50            }
51            return uniqueInstance;
52        }
53    }
54}
55

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.