Interlocked 类 简单的线程安全操作类

Interlocked.Increment  递增

Interlocked.Decrement 递减

 returnvalue  =   Interlocked.Exchange(ref valuie1, valuie2);  // 将 value2 值给 covaluie1unt,然后返回 value1 中原来的值

 returnvalue  =   Interlocked.CompareExchange (ref valuie1, valuie2,valuie3); // value1 与 value3比较,想等,则value2赋值给value1,同时方法返回value1原来的值

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ThreadPoolTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.Unicode;


            //InterLocked

            //Counter counter = new Counter();

            //Thread d1 = new Thread(()=> {
            //    for(int i=0;i<=100;i++)
            //    {
            //        int old=counter.add();
            //        Console.WriteLine("当前值:"+counter.count+ "    add old :" + old);
            //    }
            //});
            //Thread d2 = new Thread(() => {
            //    for (int i = 0; i <= 100; i++)
            //    {
            //        int old = counter.remove();
            //        Console.WriteLine("当前值:" + counter.count + "    remove old :" + old);
            //    }
            //});
            //d1.Start();
            //d2.Start();

            //测试 Exchange 方法

            Counter counter = new Counter();

            Thread d1 = new Thread(() =>
            {
                for (int i = 0; i <= 100; i++)
                {
                    Random rd = new Random();
                    int newint = rd.Next(0, 100);
                                                        // valuie1      value2 
                    int oldint = Interlocked.Exchange(ref counter.count, newint);  // 将 value2 值给 covaluie1unt,然后返回 value1 中原来的值
                    Console.WriteLine("当前值:" + counter.count + "      old :" + oldint);
                }
            });
            Thread d2 = new Thread(() =>
            {
                for (int i = 0; i <= 100; i++)
                {
                    Random rd = new Random();
                    int newint = rd.Next(0, 100);
                    int oldint = Interlocked.Exchange(ref counter.count, newint);
                    Console.WriteLine("当前值:" + counter.count + "      old2 :" + oldint);
                }
            });
            d1.Start();
            d2.Start();

            //测试 Interlocked.CompareExchange

            //Counter counter = new Counter();

            //Thread d1 = new Thread(() =>
            //{
            //    for (int i = 0; i <= 100; i++)
            //    {
            //                                                    // value1   value2   value3  
            //        int oldint = Interlocked.CompareExchange (ref counter.count, 1,0); // value1 与 value3比较,想等,则value2赋值给value1,同时方法返回value1原来的值
            //        Console.WriteLine("当前值:" + counter.count + "      old :" + oldint);
            //    }
            //});
            //Thread d2 = new Thread(() =>
            //{
            //    for (int i = 0; i <= 100; i++)
            //    {
            //        int oldint = Interlocked.CompareExchange(ref counter.count, 0, 1);
            //        Console.WriteLine("当前值:" + counter.count + "      old2 :" + oldint);
            //    }
            //});
            //d1.Start();
            //d2.Start();

            Console.ReadKey();
        }
        static void ThreadProc(Object stateInfo)
        {
            Console.WriteLine("Hello from the ThreadPool");
        }

    }
    public class Counter
    {
        public int count = 0;

        public int add()
        {
            int oldcount=Interlocked.Increment(ref count);
            return oldcount;
        }

        public int remove()
        {
            int oldcount = Interlocked.Decrement(ref count);
            return oldcount;
        }
    }

}

  

posted @ 2021-09-06 20:41  zq爱生活爱代码  阅读(160)  评论(0编辑  收藏  举报