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

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    
    namespace ProduceConsume
    {
        class Program
        {
            static void Main(string[] args)
            {
                // 创建大小为100的数据库连接对象池
                MySqlConnection[] connArr = new MySqlConnection[100];
    
                // 数组索引
                int index = -1;
    
                // 创建10个消费者
                for (int i = 0; i < 10; i++)
                {
                    // 其中的一个消费者
                    Thread thread = new Thread(() => {
                        while (true)
                        {
                            lock (connArr)
                            {
                                if (index >= 0)
                                {
                                    Console.WriteLine("{0},正在消费第{0}个连接对象...", 
                                        Thread.CurrentThread.Name,  index);
                                    connArr[index] = null;
                                    index--;
                                }
                                else
                                {
                                    Console.WriteLine("连接对象使用完毕!");
                                } 
                            }
                            Thread.Sleep(200);
                        }
    
                    });
                    
                    thread.Name = "我是消费线程" + i;
                    thread.IsBackground = true;
                    thread.Start();
                }
    
                // 创建5个生产者
                for (int i = 0; i < 5; i++)
                {
                    // 其中的一个生产者
                    Thread thread = new Thread(() => {
                        while (true)
                        {
                            lock (connArr)
                            {
                                if (index < 100)
                                {
                                    Console.WriteLine("{0},正在生产第{1}个连接对象...",
                                        Thread.CurrentThread.Name, index + 1);
                                    connArr[index + 1] = new MySqlConnection();
                                    index++;
                                }
                                else
                                {
                                    Console.WriteLine("已生产了{0}个连接对象!", index);
                                } 
                            }
                            Thread.Sleep(100);
                        }
    
                    });
                    thread.Name = "我是生产线程" + i;
                    thread.IsBackground = true;
                    thread.Start();
                }
    
                Console.ReadKey();
            }
            
        }
    
        class MySqlConnection
        {
            public int Id { get; set; }
            public string CommandText { get; set; }
    
            public void Open() { }
        }
    }
    View Code

  • 相关阅读:
    HDU 5213 分块 容斥
    HDU 2298 三分
    HDU 5144 三分
    HDU 5145 分块 莫队
    HDU 3938 并查集
    HDU 3926 并查集 图同构简单判断 STL
    POJ 2431 优先队列
    HDU 1811 拓扑排序 并查集
    HDU 2685 GCD推导
    HDU 4496 并查集 逆向思维
  • 原文地址:https://www.cnblogs.com/shaomenghao/p/4160537.html
Copyright © 2011-2022 走看看