CLR线程、线程池、任务的这些事

简介: CLR线程 CLR使用的是Windows的线程处理能力,目前的CLR实现一个CLR线程对应于一个Windows线程 System.Threading.Thread       System.Threading.Thread t = new System.Threading.Thread(op =>       {           Console.WriteLine(op);       });       t.Start("demo");       t.Join();  CLR线程池 创建和销毁线程是一个昂贵的操作,要耗费大量时间、资源,对性能也有影响。

CLR线程

CLR使用的是Windows的线程处理能力,目前的CLR实现一个CLR线程对应于一个Windows线程

System.Threading.Thread

      System.Threading.Thread t = new System.Threading.Thread(op =>

      {

          Console.WriteLine(op);

      });

      t.Start("demo");

      t.Join();

 

CLR线程池

创建和销毁线程是一个昂贵的操作,要耗费大量时间、资源,对性能也有影响。为改善这个情况,CLR包含了代码来管理它的线程池。可将线程池想象成可由你的程序使用的一个线程集合,每个CLR一个线程池,这个线程池可由CLR控制的所有AppDomian共享。如果一个进程加载了多个CLR,每个CLR都有自己的线程池。

        System.Threading.ThreadPool.QueueUserWorkItem(op =>

        {

            Console.WriteLine(op);

        },

        "demo");

 

执行上下文

每个线程都关联一个执行上下文结构,包括:安全设置(Thread.Principal Windows身份)、宿主设置(System.Threading.HostExecutionContextManager)、逻辑调用上下文数据(System.Runtime.Remoting.Messaging.CallContextLogicSetDataLogicGetData)

默认情况下,CLR自动造成初始线程的执行上下文复制到任何辅助线程,如果需要控制上下文的复制问题,可使用System.Threading.ExecutionContext类,如:

     // Put some data into the Main thread’s logical call context

        CallContext.LogicalSetData("Name""Jeffrey");

 

        // Initiate some work to be done by a thread pool thread

        // The thread pool thread can access the logical call context data 

        ThreadPool.QueueUserWorkItem(

           state => Console.WriteLine("Name={0}"CallContext.LogicalGetData("Name")));

 

 

        // Suppress the flowing of the Main thread’s execution context

        ExecutionContext.SuppressFlow();

 

        // Initiate some work to be done by a thread pool thread

        // The thread pool thread can NOT access the logical call context data

        ThreadPool.QueueUserWorkItem(

           state => Console.WriteLine("Name={0}"CallContext.LogicalGetData("Name")));

 

        // Restore the flowing of the Main thread’s execution context in case 

        // it employs more thread pool threads in the future

        ExecutionContext.RestoreFlow();

 

协作式取消

.net提供了标准的取消操作模式

   private static void CancellingAWorkItem() {

        CancellationTokenSource cts = new CancellationTokenSource();

        // Pass the CancellationToken and the number-to-count-to into the operation

        ThreadPool.QueueUserWorkItem(o => Count(cts.Token, 1000));

 

        Console.WriteLine("Press <Enter> to cancel the operation.");

        Console.ReadLine();

        cts.Cancel();  // If Count returned already, Cancel has no effect on it

        // Cancel returns immediately, and the method continues running here...

 

        Console.ReadLine();  // For testing purposes

    }

    private static void Count(CancellationToken token, Int32 countTo) {

        for (Int32 count = 0; count < countTo; count++) {

            if (token.IsCancellationRequested) {

                Console.WriteLine("Count is cancelled");

                break// Exit the loop to stop the operation

            }

 

            Console.WriteLine(count);

            Thread.Sleep(200);   // For demo, waste some time

        }

        Console.WriteLine("Count is done");

    }

任务

ThreadPool.QueueUserWorkItem虽然简单,担忧如下限制:

Ø  没有内建的机制知道操作什么时候完成

Ø  没有内建的机制在操作完成时获得一个返回值

System.Threading.Tasks下的类型可解决这个问题

      Task<Int32> t = new Task<Int32>(n => Sum((Int32)n), 10000);

 

        // You can start the task sometime later

        t.Start();

 

        // Optionally, you can explicitly wait for the task to complete

        t.Wait(); // FYI: Overloads exist accepting a timeout/CancellationToken

 

        // Get the result (the Result property internally calls Wait) 

        Console.WriteLine("The sum is: " + t.Result);   // An Int32 value

 

任务的取消使用“协作式取消”方式,CancellationTokenSource

 

一个任务完成时自动启动另一个新任务

Task.ContinueWith

 

任务可以启动子任务

 

任务工厂

         有时,可能需要创建一组Task对象来共享相同的状态,为了避免机械的将相同的参数传给每个Task的构造器,可以创建一个任务工厂来封装通用的状态。

 

 

参考

Clr Via C# 25 26

http://transbot.blog.163.com

http://ys-f.ys168.com/?CLR_via_CSharp_3rd_Edition_Code_by_Jeffrey_Richter.zip_55bism1e0e7bkisjthit2bso0cm5bs4bs1b5bktnql0c0bu22f05f12z

相关文章
|
1月前
|
Java C#
C#学习系列相关之多线程(五)----线程池ThreadPool用法
C#学习系列相关之多线程(五)----线程池ThreadPool用法
|
1月前
|
Java
线程池中的空余线程是如何被回收的
线程池中的空余线程是如何被回收的
27 1
|
1月前
|
算法 调度 索引
什么是多任务和线程?用线程写的一个udp同步聊天器
什么是多任务和线程?用线程写的一个udp同步聊天器
30 0
|
1月前
|
数据采集 存储 Java
「多线程大杀器」Python并发编程利器:ThreadPoolExecutor,让你一次性轻松开启多个线程,秒杀大量任务!
「多线程大杀器」Python并发编程利器:ThreadPoolExecutor,让你一次性轻松开启多个线程,秒杀大量任务!
|
23天前
|
存储 算法 Java
【C/C++ 线程池设计思路】 深入探索线程池设计:任务历史记录的高效管理策略
【C/C++ 线程池设计思路】 深入探索线程池设计:任务历史记录的高效管理策略
70 0
|
13天前
|
Java Spring
定时任务里面的任务多线程操作
该内容是关于Spring Boot中配置异步任务和定时任务的代码示例。首先通过`@Configuration`和`@EnableAsync`开启异步支持,然后定义线程池,如使用`ThreadPoolExecutor`并设置核心线程数、最大线程数等参数。接着,在需要异步执行的方法上添加`@Async`注解。此外,通过`@EnableScheduling`开启定时任务,并使用`@Scheduled`定义具体任务和执行周期。若需指定多个线程池,可以创建不同的`Executor` bean,并在`@Async`中指定线程池名称。
19 2
|
20天前
|
Java 测试技术 Python
Python开启线程和线程池的方法
Python开启线程和线程池的方法
14 0
Python开启线程和线程池的方法
|
30天前
|
负载均衡 Java 数据处理
【C++ 并发 线程池】轻松掌握C++线程池:从底层原理到高级应用(三)
【C++ 并发 线程池】轻松掌握C++线程池:从底层原理到高级应用
51 2
|
30天前
|
存储 监控 Java
【C++ 并发 线程池】轻松掌握C++线程池:从底层原理到高级应用(二)
【C++ 并发 线程池】轻松掌握C++线程池:从底层原理到高级应用
37 1
|
30天前
|
负载均衡 安全 Java
【C++ 并发 线程池】轻松掌握C++线程池:从底层原理到高级应用(一)
【C++ 并发 线程池】轻松掌握C++线程池:从底层原理到高级应用
57 2

热门文章

最新文章