• System.Threading.Interlocked.CompareChange使用


     1 class CompareChange
     2     {
     3         private static int i = 0;
     4         public static System.Threading.AutoResetEvent changeSignal = new System.Threading.AutoResetEvent(false);//同步信号
     5 
     6         public static void test()
     7         {
     8             ThreadSafe ts = new ThreadSafe();
     9 
    10             System.Threading.ThreadPool.QueueUserWorkItem((obj) =>
    11             {
    12                 Console.WriteLine("modifying-thread is waiting for signal");
    13                 CompareChange.changeSignal.WaitOne();
    14                 Console.WriteLine("modifying-thread received the signal");
    15                 CompareChange.i++;
    16                 ts.Total = CompareChange.i;
    17                 Console.WriteLine("modifying-thread modify the CompareChange.i");
    18                 CompareChange.changeSignal.Set();
    19                 Console.WriteLine("modifying-thread send the complete signal");
    20             });
    21 
    22             System.Threading.ThreadPool.QueueUserWorkItem((obj) =>
    23             {
    24                 ts.AddToTotal(20);
    25             });
    26             
    27             Console.ReadLine();
    28         }
    29     }
    30 
    31     public class ThreadSafe
    32     {
    33         // totalValue contains a running total that can be updated 
    34         // by multiple threads. It must be protected from unsynchronized  
    35         // access. 
    36         private int totalValue = 0;
    37         private int times = 0;
    38         // The Total property returns the running total. 
    39         public int Total
    40         {
    41             get { return totalValue; }
    42             set { totalValue = value; }
    43         }
    44 
    45         // AddToTotal safely adds a value to the running total. 
    46         public int AddToTotal(int addend)
    47         {
    48             int initialValue, computedValue;
    49             do
    50             {
    51                 this.times++;
    52                 if (this.times == 2)
    53                     Console.WriteLine("orignal value has changed.it's the "+this.times.ToString()+"th times to run the process");
    54                 // Save the current running total in a local variable.
    55                 initialValue = totalValue;
    56 
    57                 Console.WriteLine("sending the modifying signal");
    58                 CompareChange.changeSignal.Set();
    59 
    60                 // Add the new value to the running total.
    61                 computedValue = initialValue + addend;
    62 
    63                 Console.WriteLine("waiting for modifying-completely signal");
    64                 CompareChange.changeSignal.WaitOne();
    65 
    66                 // CompareExchange compares totalValue to initialValue. If 
    67                 // they are not equal, then another thread has updated the 
    68                 // running total since this loop started. CompareExchange 
    69                 // does not update totalValue. CompareExchange returns the 
    70                 // contents of totalValue, which do not equal initialValue, 
    71                 // so the loop executes again.
    72             } while (initialValue != System.Threading.Interlocked.CompareExchange(
    73                 ref totalValue, computedValue, initialValue));
    74             // If no other thread updated the running total, then  
    75             // totalValue and initialValue are equal when CompareExchange 
    76             // compares them, and computedValue is stored in totalValue. 
    77             // CompareExchange returns the value that was in totalValue 
    78             // before the update, which is equal to initialValue, so the  
    79             // loop ends. 
    80             Console.WriteLine("process is over");
    81             // The function returns computedValue, not totalValue, because 
    82             // totalValue could be changed by another thread between 
    83             // the time the loop ends and the function returns. 
    84             return computedValue;
    85         }
    86     }

    运行结果:

    modifying-thread is waiting for signal
    sending the modifying signal
    waiting for modifying-completely signal
    modifying-thread received the signal
    modifying-thread modify the CompareChange.i
    modifying-thread send the complete signal
    orignal value has changed.it's the 2th times to run the process
    sending the modifying signal
    waiting for modifying-completely signal
    process is over

  • 相关阅读:
    安卓监听帧动画结束
    零基础学python-13.4 文件上使用列表解析与列表解析扩展
    零基础学python-13.3 列表解析简介与步骤分解
    零基础学python-13.2 手动迭代:iter和next
    零基础学python-13.1 迭代器简介与文件迭代器
    零基础学python-12.6 使用for和zip来并行使用多个序列
    零基础学python-12.5 修改列表的误区以及使用for和range修改列表
    零基础学python-12.4 while、for与range联合使用
    零基础学python-12.3 for循环
    零基础学python-12.2 关键字pass,else,break,continue
  • 原文地址:https://www.cnblogs.com/javaleon/p/3921633.html
Copyright © 2020-2023  润新知