当前位置: 首页>>代码示例>>C#>>正文


C# Interlocked.Decrement方法代码示例

本文整理汇总了C#中System.Threading.Interlocked.Decrement方法的典型用法代码示例。如果您正苦于以下问题:C# Interlocked.Decrement方法的具体用法?C# Interlocked.Decrement怎么用?C# Interlocked.Decrement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Threading.Interlocked的用法示例。


在下文中一共展示了Interlocked.Decrement方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Object

//引入命名空间
using System;
using System.Threading;

public class Example
{
   const int LOWERBOUND = 0;
   const int UPPERBOUND = 1001;
   
   static Object lockObj = new Object();
   static Random rnd = new Random();
   static CountdownEvent cte;
   
   static int totalCount = 0;
   static int totalMidpoint = 0;
   static int midpointCount = 10000;

   public static void Main()
   {
      cte = new CountdownEvent(1);
      // Start three threads. 
      for (int ctr = 0; ctr <= 2; ctr++) {
         cte.AddCount();
         Thread th = new Thread(GenerateNumbers);
         th.Name = "Thread" + ctr.ToString();
         th.Start();
      }
      cte.Signal();
      cte.Wait();
      Console.WriteLine();
      Console.WriteLine("Total midpoint values:  {0,10:N0} ({1:P3})",
                        totalMidpoint, totalMidpoint/((double)totalCount));
      Console.WriteLine("Total number of values: {0,10:N0}", 
                        totalCount);                  
   }

   private static void GenerateNumbers()
   {
      int midpoint = (UPPERBOUND - LOWERBOUND) / 2;
      int value = 0;
      int total = 0;
      int midpt = 0;
      
      do {
         lock (lockObj) {
            value = rnd.Next(LOWERBOUND, UPPERBOUND);
         }
         if (value == midpoint) { 
            Interlocked.Decrement(ref midpointCount);
            midpt++;
         }
         total++;    
      } while (midpointCount > 0);
      
      Interlocked.Add(ref totalCount, total);
      Interlocked.Add(ref totalMidpoint, midpt);
      
      string s = String.Format("Thread {0}:\n", Thread.CurrentThread.Name) +
                 String.Format("   Random Numbers: {0:N0}\n", total) + 
                 String.Format("   Midpoint values: {0:N0} ({1:P3})", midpt, 
                               ((double) midpt)/total);
      Console.WriteLine(s);
      cte.Signal();
   }
}
开发者ID:.NET开发者,项目名称:System.Threading,代码行数:65,代码来源:Interlocked.Decrement

输出:

Thread Thread2:
Random Numbers: 3,204,021
Midpoint values: 3,156 (0.099 %)
Thread Thread0:
Random Numbers: 4,073,592
Midpoint values: 4,015 (0.099 %)
Thread Thread1:
Random Numbers: 2,828,192
Midpoint values: 2,829 (0.100 %)

Total midpoint values:      10,000 (0.099 %)
Total number of values: 10,105,805

示例2: Object

//引入命名空间
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   const int LOWERBOUND = 0;
   const int UPPERBOUND = 1001;
   
   static Object lockObj = new Object();
   static Random rnd = new Random();
   
   static int totalCount = 0;
   static int totalMidpoint = 0;
   static int midpointCount = 50000;

   public static async Task Main()
   {
      List<Task> tasks = new List<Task>();

      // Start three tasks. 
      for (int ctr = 0; ctr <= 2; ctr++) 
         tasks.Add(Task.Run( () => { int midpoint = (UPPERBOUND - LOWERBOUND) / 2;
                                     int value = 0;
                                     int total = 0;
                                     int midpt = 0;
      
                                     do {
                                        lock (lockObj) {
                                           value = rnd.Next(LOWERBOUND, UPPERBOUND);
                                        }
                                        if (value == midpoint) { 
                                           Interlocked.Decrement(ref midpointCount);
                                           midpt++;
                                        }
                                        total++;    
                                     } while (midpointCount > 0 );
                                          
                                     Interlocked.Add(ref totalCount, total);
                                     Interlocked.Add(ref totalMidpoint, midpt);
                                          
                                     string s = String.Format("Task {0}:\n", Task.CurrentId) +
                                                String.Format("   Random Numbers: {0:N0}\n", total) + 
                                                String.Format("   Midpoint values: {0:N0} ({1:P3})", midpt, 
                                                              ((double) midpt)/total);
                                     Console.WriteLine(s); 
                                   } ));

      await Task.WhenAll(tasks.ToArray());

      Console.WriteLine();
      Console.WriteLine("Total midpoint values:  {0,10:N0} ({1:P3})",
                        totalMidpoint, totalMidpoint/((double)totalCount));
      Console.WriteLine("Total number of values: {0,10:N0}", 
                        totalCount);                  
   }
}
开发者ID:.NET开发者,项目名称:System.Threading,代码行数:59,代码来源:Interlocked.Decrement

输出:

Task 1:
Random Numbers: 24,530,624
Midpoint values: 24,675 (0.101 %)
Task 2:
Random Numbers: 7,079,718
Midpoint values: 7,093 (0.100 %)
Task 3:
Random Numbers: 18,284,617
Midpoint values: 18,232 (0.100 %)

Total midpoint values:      50,000 (0.100 %)
Total number of values: 49,894,959

示例3: Main

//引入命名空间
using System;
using System.Threading;

class MainClass
{
    public static void Main()
    {
        int firstInt = 25;
        int secondInt = 80;

        Console.WriteLine("firstInt initial value = {0}", firstInt);
        Console.WriteLine("secondInt initial value = {0}", secondInt);

        Interlocked.Decrement(ref firstInt);

        Console.WriteLine("firstInt after decrement = {0}", firstInt);


    }
}
开发者ID:C#程序员,项目名称:System.Threading,代码行数:21,代码来源:Interlocked.Decrement


注:本文中的System.Threading.Interlocked.Decrement方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。