首先咱们可以看一下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模式中对多线程的优化
1
using System;
2
using System.Configuration;
3
4
namespace HeadFirstDesignPatterns.Singleton.InterestRate
5
2
3
4
5