代码改变世界

简单示例代码,多线程见协作

  chris-shao  阅读(259)  评论(0编辑  收藏  举报
一个简单的多线程示例,主要目的就是给一个账户加钱和给账户减钱,当账户余额小于一个值的时候,减钱的线程必须停止减钱,等有足够钱时,才能继续减钱。
复制代码
using System;
using System.Collections.Generic;
using System.Text;
/*
 多线程演示实例
 
*/
namespace MultThreadExample
{
    class Program
    {
        static Account account = new Account();
        static void Main(string[] args)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(add));
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(subtract));
            Console.ReadLine();
        }
        static void add(object obj)
        {
            while (true)
            {
                account.Add();
                System.Threading.Thread.Sleep(1000);
            }
        }
        static void subtract(object obj)
        {
            while (true)
            {
                account.subtract();
                System.Threading.Thread.Sleep(100);
            }
        }
    }
    class Account
    {
        private object m_lock=new object();
        private int _count=0;
        public void Add()
        {
            lock (m_lock)
            {

                _count = _count + 1;
                Console.WriteLine("count:" + _count + "\t Add");
                System.Threading.Monitor.PulseAll(m_lock);

            }
        }
        public void subtract()
        {
            lock (m_lock)
            {

                while (_count <= 5)
                {
                    System.Threading.Monitor.Wait(m_lock);
                }
                _count--;
                Console.WriteLine("count:" + _count + "\t subtract");
            }

        }
    }
}
复制代码

稍做修改,生产者消费者问题。

 

复制代码
namespace ConsoleApplication1
{
    class Program
    {
        static Account account = new Account();
        static void Main(string[] args)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(add));
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(subtract));
            Console.ReadLine();
        }
        static void add(object obj)
        {
            while (true)
            {
                account.Add();
                System.Threading.Thread.Sleep(2000);
            }
        }
        static void subtract(object obj)
        {
            while (true)
            {
                account.subtract();
                System.Threading.Thread.Sleep(8000);
            }
        }
    }
    class Account
    {
        private object m_lock=new object();
        private System.Collections.ArrayList _ary = new System.Collections.ArrayList();
        public void Add()
        {
            lock (m_lock)
            {
                while (_ary.Count >= 5)
                {
                    //队列满
                    Console.WriteLine("队列满,等待消费:" );
                    System.Threading.Monitor.Wait(m_lock);
                }
                _ary.Add("product");
                Console.WriteLine("生产者 增加: 队列长度:" + _ary.Count);
                System.Threading.Monitor.Pulse(m_lock);
                
            }
        }
        public void subtract()
        {
            lock (m_lock)
            {

                while (_ary.Count <= 0)
                {
                    Console.WriteLine("队列空,等待生产:");
                    System.Threading.Monitor.Wait(m_lock);
                }
                _ary.RemoveAt(0);
                Console.WriteLine("消费者 减少: 队列长度:" + _ary.Count);
                System.Threading.Monitor.Pulse(m_lock);
            }

        }
    }
}
复制代码

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示