zoukankan      html  css  js  c++  java
  • C#线程池

     class Program
        {
            // 使用BeginXXX/EndXXX和IAsyncResult对象的方式被称为异步编程模型(APM模式)
            delegate string RunOnThreadPool(out int threadId);
    
            static void Main(string[] args)
            {
                int threadId = 0;
                // 给委托变量赋值
                RunOnThreadPool poolDelegate = Test;
    
                var t = new Thread(() => { Test(out threadId); });
                t.Start();
                t.Join();
                Console.WriteLine($"1.返回的线程Id:{threadId}");
    
                // 通过调用委托变量的BeginInvoke方法来运行委托(执行Test方法)
                IAsyncResult ar = poolDelegate.BeginInvoke(out threadId, Callback, "异步委托调用");
                ar.AsyncWaitHandle.WaitOne();
                // 调用委托的EndInvoke会等待异步操作(Test方法)完成
                // 异步操作执行完成之后,开始执行回掉函数;异步操作和回掉函数很可能会被线程池中同一个工作线程执行
                string result = poolDelegate.EndInvoke(out threadId, ar);
                Console.WriteLine($"2.返回的线程Id:{threadId}");
                Console.WriteLine($"返回值:{result}");
    
                Console.ReadKey();
            }
    
            static void Callback(IAsyncResult ar)
            {
                Console.WriteLine("开始执行回掉函数...");
                Console.WriteLine($"异步状态:{ar.AsyncState}");
                Console.WriteLine($"是否为线程池中的线程:{Thread.CurrentThread.IsThreadPoolThread}");
                Console.WriteLine($"线程池工作线程Id:{Thread.CurrentThread.ManagedThreadId}");
            }
    
            static string Test(out int threadId)
            {
                Console.WriteLine("开始测试方法...");
                Console.WriteLine($"是否为线程池中的线程:{Thread.CurrentThread.IsThreadPoolThread}");
                Thread.Sleep(TimeSpan.FromSeconds(2));
                threadId = Thread.CurrentThread.ManagedThreadId;
                return $"线程Id:{threadId}";
            }
        }
    

      向线程池中放入异步操作

     class Program
        {
            static void Main(string[] args)
            {
                ThreadPool.QueueUserWorkItem(AsyncOperation);
                Thread.Sleep(TimeSpan.FromSeconds(2));
                ThreadPool.QueueUserWorkItem(AsyncOperation, "async state");
                Thread.Sleep(TimeSpan.FromSeconds(2));
    
                ThreadPool.QueueUserWorkItem(state =>
                {
                    Console.WriteLine($"操作状态:{state}");
                    Console.WriteLine($"工作线程Id:{Thread.CurrentThread.ManagedThreadId}");
                    Thread.Sleep(TimeSpan.FromSeconds(2));
                }, "lambda state");
    
                const int x = 1;
                const int y = 2;
                const string lambdaState = "lambda state 2";
                // 使用闭包机制,无需传递lambda表达式的状态参数
                ThreadPool.QueueUserWorkItem(_ =>
                {
                    Console.WriteLine($"操作状态:{x + y},{lambdaState}");
                    Console.WriteLine($"工作线程Id:{Thread.CurrentThread.ManagedThreadId}");
                    Thread.Sleep(TimeSpan.FromSeconds(2));
                });
    
                Console.ReadKey();
            }
    
            static void AsyncOperation(object state)
            {
                Console.WriteLine($"操作状态:{state ?? "(null)"}");
                Console.WriteLine($"工作线程Id:{Thread.CurrentThread.ManagedThreadId}");
                Thread.Sleep(TimeSpan.FromSeconds(2));
            }
        }
    

      线程池与并行度:

  • 相关阅读:
    Java虚拟机的内存模型
    JAVA 对文件的操作
    JAVA 读取 YAML 文件
    Nginx 502 问题解决 及 安装
    Python pdb 调试 命令
    pycharm设置鼠标控制字体大小
    ISO9126 软件质量模型
    人生苦短我学Java9面向对象三大特性之多态 广深
    Golang微服务入门到精通之路3类的封装/继承/多态/接口类型 广深
    人生苦短我学Java10final关键字/代码块/抽象类 广深
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/12993590.html
Copyright © 2011-2022 走看看