黑马程序员--interlocked(互锁)

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------interlocked(互锁)



当多个线程需要使用统一资源时,就需要进行线程同步,例如对一个CHAR类型变量的读和写



interlocked类的部分方法
read() 读取计数器的值
Increment() 使计数器增加1
Decrement()使计数器减小1;
Add() 使计数器增加指定的值
Exchange() 把计数器设置为指定的值
CompareExchange()  先把计数器和某个值进行比较,若相等,就把计数器设为指定值

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

namespace 线程同步interlocked
{
    class Program
    {
        private static char result;
        private static string a = "青青子衿,悠悠我心!";

        static void Main(string[] args)
        {
            ThreadStart ts=write;
            ThreadStart ts2 = read;
            Thread readThread = new Thread(ts2);
            Thread writeThread = new Thread(ts);
            writeThread.Start();
            readThread.Start();

        }
        
        static void write()
        {
            for (int i = 0; i < a.Length; i++)
            {
                result = a[i];
                Thread.Sleep(20);
            }
        }
        static void read()
        {
            for (int i = 0; i < a.Length; i++)
            {
                char b = result;
                Console.Write(b);
                Thread.Sleep(20);
            }

        }
    }
}

这样输出后会有错误


 


使用互锁后就可以正常输出了

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

namespace 线程同步interlocked
{
    class Program
    {
        private static char result;
        private static string a = "青青子衿,悠悠我心!";

        static void Main(string[] args)
        {
            ThreadStart ts=write;
            ThreadStart ts2 = read;
            Thread readThread = new Thread(ts2);
            Thread writeThread = new Thread(ts);
            writeThread.Start();
            readThread.Start();

        }
        static long num = 0;
        static void write()
        {
            for (int i = 0; i < a.Length; i++)
            {
                //while (Interlocked.Read(ref num) == 1)
                //{ Thread.Sleep(10); }
                while (num == 1)
                { Thread.Sleep(10); }
                result = a[i];
                //Interlocked.Increment(ref num);
                num += 1;
            }
        }
        static void read()
        {
            for (int i = 0; i < a.Length; i++)
            {
                //while (Interlocked.Read(ref num) == 0)
                //{ Thread.Sleep(10); }
                while (num == 0)
                { Thread.Sleep(10); }
                char b = result;
                Console.Write(b);
                num = num - 1;
                //Interlocked.Decrement(ref num);
            }

        }
    }
}







---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------详细请查看: http://net.itheima.com/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值