C# BlockingCollection实现线程间通信

C# BlockingCollection实现线程间通信

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication.ExtendedProtection;
using System.Text;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;

namespace ConsoleApp1
{
   
    class Program
    {
   
        static void Main(string[] args)
        {
   
            sender.Start(1);
            receive1.Start(2);
            receive2.Start(3);
            sender.Join();
            Thread.Sleep(3000);
            receive1.Interrupt();
            receive2.Interrupt();

            receive1.Join();
            receive2.Join();    


            Console.ReadKey();
        }
        #region BlockingCollection实现线程间通信
        static BlockingCollection<Message> blockCollenction = new BlockingCollection<Message>(new ConcurrentQueue<Message>());
        static Thread sender = new Thread(SendMsg);

        static Thread receive1 = new Thread(ReceiveMsg);
        static Thread receive2 = new Thread(ReceiveMsg);

        static void SendMsg(object id)
        {
   
            for (int i = 0; i < 20; i++)
            {
   
                blockCollenction.Add(new Message((int)id, i.ToString()));
                Console.WriteLine($"【线程{
     id}】发送了【{
     i}】");
            }
        }

        static void ReceiveMsg(object id)
        {
   
            try
            {
   
                while (true)
                {
   
                    Message message =  blockCollenction.Take();
                    Console.WriteLine($"【线程{
     id}】从【线程{
     message.id}】接收了【{
     message.content}】");
                    Thread.Sleep(1);
                }
            }
            catch (ThreadInterruptedException ex)
            {
   
                Console.WriteLine($"接收结束");
            }
        }

    }
}

class Message
{
   
    public int id;
    public string content;

    public Message(int _id, string _content)
    {
   
        id = _id;
        content = _content;
    }
}

#endregion


相关推荐

  1. C# BlockingCollection实现线通信

    2023-12-15 19:48:02       60 阅读
  2. C# Channel实现线通信

    2023-12-15 19:48:02       51 阅读
  3. C++ 并发编程指南(8)线通信

    2023-12-15 19:48:02       41 阅读
  4. 【17】Android 线通信(二) - Handler

    2023-12-15 19:48:02       21 阅读
  5. 【18】Android 线通信(三) - Handler

    2023-12-15 19:48:02       20 阅读
  6. Perl并发编程秘籍:线通信的艺术

    2023-12-15 19:48:02       22 阅读
  7. A类中创建posix线线如何通信

    2023-12-15 19:48:02       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-15 19:48:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-15 19:48:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-15 19:48:02       82 阅读
  4. Python语言-面向对象

    2023-12-15 19:48:02       91 阅读

热门阅读

  1. vue 导出el-table选择的数据使用笔记

    2023-12-15 19:48:02       52 阅读
  2. Linux中用rpm管理软件

    2023-12-15 19:48:02       42 阅读
  3. WPF中DataGrid的表格常见显示优化

    2023-12-15 19:48:02       57 阅读
  4. 网络安全之计算机网络基础知识<二>

    2023-12-15 19:48:02       59 阅读
  5. ORACLE释放表空间中的空闲数据文件

    2023-12-15 19:48:02       48 阅读
  6. 富文本内容图片点击实现多图预览

    2023-12-15 19:48:02       60 阅读
  7. Linux anacron命令 检测长期不执行的定时任务

    2023-12-15 19:48:02       44 阅读
  8. 30天精通Nodejs--第十三天:MySQL2

    2023-12-15 19:48:02       57 阅读
  9. 计算机网络中的通信子网主要有哪些功能?

    2023-12-15 19:48:02       64 阅读