zoukankan      html  css  js  c++  java
  • 生产者消费者模式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace M_生产者消费者模式
    {
        class Program
        {
            static void Main(string[] args)
            {
                
               Prodeuct[] produs=new Prodeuct[100];
    
              object m_lock=new object();
               int index = 0;
                //定义5个生产者
                for(int i =0;i<5;i++)
                {
                    new Thread(() => {
                        while (true)
                        {
                            lock (m_lock)
                            {
                                if (index < 99)
                                {
                                    Prodeuct p = new Prodeuct();
                                    produs[index] = p;
                                    Console.WriteLine("生成一个产品" + Thread.CurrentThread.ManagedThreadId);
                                    index++;
                                }
                            }
                          
                        }
                                     
                    }).Start();
                    //定义5个消费者
                    for (int j = 0; j < 5; j++)
                    {
                        new Thread(() =>
                        {
    
                            while (true)
                            {
                                lock (m_lock)
                                {
                                    if (index > 0)
                                    {
                                        Prodeuct p = produs[index - 1] = null;
                                        Console.WriteLine("消耗一个产品" + Thread.CurrentThread.ManagedThreadId);
                                        index--;
                                    }
                                }
                                Thread.Sleep(100);
                            }
    
    
                        }).Start();
    
                    }
    
                }
            }
           public class Prodeuct
           {
    
           }
        }
    }
    

      

  • 相关阅读:
    vi常用操作
    Python练习题
    Jmeter也能IP欺骗!
    mysql主从配置
    性能测试之mysql监控、优化
    Git 命令
    Chrome——F12 谷歌开发者工具详解
    Appscan
    微信群发红包抢红包设计测试用例
    MySQL基础篇(1)SQL基础
  • 原文地址:https://www.cnblogs.com/cdaq/p/4938576.html
Copyright © 2011-2022 走看看