volatile 关键字指示一个字段可以由多个同时执行的线程修改。 声明为 volatile 的字段不受编译器优化(假定由单个线程访问)的限制。 这样可以确保该字段在任何时间呈现的都是最新的值。
volatile 修饰符通常用于由多个线程访问但不使用 lock 语句对访问进行序列化的字段。
volatile 关键字可应用于以下类型的字段:
引用类型。
指针类型(在不安全的上下文中)。 请注意,虽然指针本身可以是可变的,但是它指向的对象不能是可变的。 换句话说,您无法声明“指向可变对象的指针”。
类型,如 sbyte、byte、short、ushort、int、uint、char、float 和 bool。
具有以下基类型之一的枚举类型:byte、sbyte、short、ushort、int 或 uint。
已知为引用类型的泛型类型参数。
可变关键字仅可应用于类或结构字段。 不能将局部变量声明为 volatile。
下面的示例说明如何将公共字段变量声明为 volatile。
1 2 3 4 5 6 7 8 9 |
<span style="color: blue;">class</span> VolatileTest { <span style="color: blue;">public</span> <span style="color: blue;">volatile</span> <span style="color: blue;">int</span> i; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Test(<span style="color: blue;">int</span> _i) { i = _i; } } |
下面的示例演示如何创建辅助线程,并用它与主线程并行执行处理。 有关多线程处理的背景信息,请参见托管线程处理和线程处理(C# 和 Visual Basic)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
<span style="color: blue;">using</span> System; <span style="color: blue;">using</span> System.Threading; <span style="color: blue;">public</span> <span style="color: blue;">class</span> Worker { <span style="color: green;">// This method is called when the thread is started.</span> <span style="color: blue;">public</span> <span style="color: blue;">void</span> DoWork() { <span style="color: blue;">while</span> (!_shouldStop) { Console.WriteLine(<span style="color: #a31515;">"Worker thread: working..."</span>); } Console.WriteLine(<span style="color: #a31515;">"Worker thread: terminating gracefully."</span>); } <span style="color: blue;">public</span> <span style="color: blue;">void</span> RequestStop() { _shouldStop = <span style="color: blue;">true</span>; } <span style="color: green;">// Keyword volatile is used as a hint to the compiler that this data</span> <span style="color: green;">// member is accessed by multiple threads.</span> <span style="color: blue;">private</span> <span style="color: blue;">volatile</span> <span style="color: blue;">bool</span> _shouldStop; } <span style="color: blue;">public</span> <span style="color: blue;">class</span> WorkerThreadExample { <span style="color: blue;">static</span> <span style="color: blue;">void</span> Main() { <span style="color: green;">// Create the worker thread object. This does not start the thread.</span> Worker workerObject = <span style="color: blue;">new</span> Worker(); Thread workerThread = <span style="color: blue;">new</span> Thread(workerObject.DoWork); <span style="color: green;">// Start the worker thread.</span> workerThread.Start(); Console.WriteLine(<span style="color: #a31515;">"Main thread: starting worker thread..."</span>); <span style="color: green;">// Loop until the worker thread activates.</span> <span style="color: blue;">while</span> (!workerThread.IsAlive) ; <span style="color: green;">// Put the main thread to sleep for 1 millisecond to</span> <span style="color: green;">// allow the worker thread to do some work.</span> Thread.Sleep(1); <span style="color: green;">// Request that the worker thread stop itself.</span> workerObject.RequestStop(); <span style="color: green;">// Use the Thread.Join method to block the current thread </span> <span style="color: green;">// until the object's thread terminates.</span> workerThread.Join(); Console.WriteLine(<span style="color: #a31515;">"Main thread: worker thread has terminated."</span>); } <span style="color: green;">// Sample output:</span> <span style="color: green;">// Main thread: starting worker thread...</span> <span style="color: green;">// Worker thread: working...</span> <span style="color: green;">// Worker thread: working...</span> <span style="color: green;">// Worker thread: working...</span> <span style="color: green;">// Worker thread: working...</span> <span style="color: green;">// Worker thread: working...</span> <span style="color: green;">// Worker thread: working...</span> <span style="color: green;">// Worker thread: terminating gracefully.</span> <span style="color: green;">// Main thread: worker thread has terminated.</span> } from:http://msdn.microsoft.com/zh-cn/library/x13ttww7.aspx |