当前位置: 移动技术网 > IT编程>开发语言>.net > .NET多线程之Thread、Task、ThreadPool、Timer

.NET多线程之Thread、Task、ThreadPool、Timer

2019年05月08日  | 移动技术网IT编程  | 我要评论

四项基本原则的内容,love可儿的秘密花园,路易革命之白雪姬

下表为多线程操作常见对象:

对象 方法/属性 描述 用途 用法 性能
thread(线程) start 启动线程,启动后线程处于system.threading.threadstate.running状态 创建线程后,启动 new thread(()=>{}).start();
sleep 将当前线程挂起指定的毫秒数。 挂起当前线程1秒 thread.sleet(1000)
abort 中止当前线程 停止后续代码运行 thread.currentthread.abort()
currentthread 获取当前线程对象 获取当前线程对象 thread.currentthread
currentprincipal 获取或设置线程的当前负责人(对基于角色的安全性而言)。 可通过claimsprincipal在当前线程中存储相关申明,abp中的defaultprincipalaccessor(iprincipalaccessor)用来传递当前登陆人信息 thread.currentthread.managedthreadid
managedthreadid 获取当前托管线程的唯一标识符。    
task(任务) run  将在线程池上运行的指定工作排队,并返回 function 所返回的任务的代理项。   task.run(()=>{})
delay  创建一个在指定的时间间隔后完成的任务。   task.delay(1000).
waitall 等待提供的所有 system.threading.tasks.task 对象完成执行过程。 等待指定task完成 task.waitall()
waitany 等待提供的任一 system.threading.tasks.task 对象完成执行过程。 等待任一一个任务完成即执行后续操作 task.waitany()
fromresult 创建成功完成返回指定结果的任务 在异步开发模式中,常用于返回一个成功的任务,以达到异步调用的方式 task.fromresult(tresult)
task<tresult> result 获取此task返回值    
threadpool(线程池) queueuserworkitem 将方法排入队列以便执行,并指定包含该方法所用数据的对象。 此方法在有线程池线程变得可用时执行。  

object data = null;
threadpool.queueuserworkitem((state) =>
{

}, data);

system.timers.timer(定时器),是对system.threading.timer封装 start 通过将 system.timers.timer.enabled 设置为 true 开始引发 system.timers.timer.elapsed 事件。    
stop 通过将 system.timers.timer.enabled 设置为 false 停止引发 system.timers.timer.elapsed 事件。    
close  释放由 system.timers.timer 占用的资源。    
autoreset

获取或设置一个布尔值,该值指示 system.timers.timer 是否应只引发一次 system.timers.timer.elapsed 事件((false) 或重复 (true))。

应设置为true  
enabled  获取或设置一个值,该值指示 system.timers.timer 是否应引发 system.timers.timer.elapsed 事件。 应设置为true  
interval  获取或设置引发 system.timers.timer.elapsed 事件的间隔(以毫秒为单位)。    

system.threading.timer(定时器)

       

 

1、thread(线程)

  官方文档:https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.thread?redirectedfrom=msdn&view=netframework-4.7.2

  原生的线程对象,性能相对较低,无法共用现有线程。每次都会创建一个新的线程,涉及到线程的销毁、创建,所以性能相对较低。不建议使用;

  示例:

 1     public void testthread()
 2         {
 3             var thread = new thread(() =>
 4             {
 5                 console.writeline($"3、async current thread id={thread.currentthread.managedthreadid.tostring("00")} thread state={thread.currentthread.threadstate.tostring()}");
 6             });
 7             console.writeline($"1、async current thread id={thread.currentthread.managedthreadid.tostring("00")} thread state={thread.threadstate.tostring()}");
 8             thread.start();
 9             console.writeline($"2、async current thread id={thread.currentthread.managedthreadid.tostring("00")} thread state={thread.threadstate.tostring()}");
10             thread.sleep(1000);
11             console.writeline($"4、async current thread id={thread.currentthread.managedthreadid.tostring("00")} thread state={thread.threadstate.tostring()}");
12             console.writeline(thread.getdomainid());
13         }

 

2、task(任务)

  官方文档:

  task是对threadpool的封装,支持等待,不直接创建线程和销毁线程,由threadpool管理创建和销毁。建议使用;

  示例:

        public void testtask()
        {
            console.writeline($"async current thread id={thread.currentthread.managedthreadid}");
            var tasks = new list<task>(100);
            for (var i = 0; i < 10; i++)
            {
                runtask(i);
            }
        }
        private task runtask(int i)
        {
            return task.run(() =>
            {
                console.writeline($"sync current thread id={thread.currentthread.managedthreadid.tostring("00")}   i={i.tostring("00")}");
            });
        }

 

3、threadpool(线程池)

  官方文档:

  类似与ado.net数据库连接池,出发点都是用于统一管理线程的创建和销毁。如果异步任务不需要等待建议使用;

  示例:

 

        public void testthreadpool()
        {
            object data = null;
            threadpool.queueuserworkitem((state) =>
            {
                console.writeline($"sync current thread id={thread.currentthread.managedthreadid.tostring("00")}   i={i.tostring("00")}");
            }, data);
        }

 

4、system.timers.timer定时器

  system.threading.timer官方文档:

  system.timers.timer官方文档:

  system.timers.timer定时器是对system.threading.timer封装,更加方便使用,使用那个看个人习惯;

  示例:

     public void testtimer()
        {
            var hubtimer = new system.timers.timer(10 * 1000)
            {
                autoreset = true,
                enabled = true,
            };
            hubtimer.elapsed += run;//注册事件
            hubtimer.elapsed -= run;//取消事件
            hubtimer.start();
        }

        public void run(object sender, elapsedeventargs e)
        {

        }

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网