• volatile 关键词


    volatile 关键字指示一个字段可以由多个同时执行的线程修改。 出于性能原因,编译器,运行时系统甚至硬件都可能重新排列对存储器位置的读取和写入。 声明了 volatile 的字段不进行这些优化。这样可以确保该字段在任何时间呈现的都是最新的值。

    volatile 关键字可应用于以下类型的字段:

    • 引用类型。
    • 指针类型(在不安全的上下文中)。 请注意,虽然指针本身可以是可变的,但是它指向的对象不能是可变的。 换句话说,不能声明“指向可变对象的指针”。
    • 简单类型,如 sbytebyteshortushortintuintcharfloat 和 bool
    • 具有以下基本类型之一的 enum 类型:bytesbyteshortushortint 或 uint
    • 已知为引用类型的泛型类型参数。
    • IntPtr 和 UIntPtr

    其他类型(包括 double 和 long)无法标记为 volatile,因为对这些类型的字段的读取和写入不能保证是原子的。 若要保护对这些类型字段的多线程访问,请使用 Interlocked 类成员或使用 lock 语句保护访问权限。

    volatile 关键字只能应用于 class 或 struct 的字段。 不能将局部变量声明为 volatile

    public class Worker
    {
        // This method is called when the thread is started.
        public void DoWork()
        {
            bool work = false;
            while (!_shouldStop)
            {
                work = !work; // simulate some work
            }
            Console.WriteLine("Worker thread: terminating gracefully.");
        }
        public void RequestStop()
        {
            _shouldStop = true;
        }
        // Keyword volatile is used as a hint to the compiler that this data
        // member is accessed by multiple threads.
        private volatile bool _shouldStop;
    }
    
    public class WorkerThreadExample
    {
        public static void Main()
        {
            // Create the worker thread object. This does not start the thread.
            Worker workerObject = new Worker();
            Thread workerThread = new Thread(workerObject.DoWork);
    
            // Start the worker thread.
            workerThread.Start();
            Console.WriteLine("Main thread: starting worker thread...");
    
            // Loop until the worker thread activates.
            while (!workerThread.IsAlive)
                ;
    
            // Put the main thread to sleep for 500 milliseconds to
            // allow the worker thread to do some work.
            Thread.Sleep(500);
    
            // Request that the worker thread stop itself.
            workerObject.RequestStop();
    
            // Use the Thread.Join method to block the current thread 
            // until the object's thread terminates.
            workerThread.Join();
            Console.WriteLine("Main thread: worker thread has terminated.");
        }
        // Sample output:
        // Main thread: starting worker thread...
        // Worker thread: terminating gracefully.
        // Main thread: worker thread has terminated.
    }
  • 相关阅读:
    Meteor + node-imap(nodejs) + mailparser(nodejs) 实现完整收发邮件
    详解log4j2(上)
    循序渐进之Spring AOP(6)
    循序渐进之Spring AOP(5)
    循序渐进之Spring AOP(3)
    循序渐进之Spring AOP(4)
    循序渐进之Spring AOP(2)
    循序渐进之Spring AOP(1)
    通俗的解释JAVA wait/notify机制
    开发高性能JAVA应用程序基础(内存篇)
  • 原文地址:https://www.cnblogs.com/jshchg/p/11671580.html
Copyright © 2020-2023  润新知