.net 多线程

.net 多线程

1.线程声明和启动

Thread th = new Thread(new ThreadStart(Down()));  th.Start();

Down()是具体要执行的方法。

ThreadStart是执行无参方法

ParameterizedThreadStart是执行有参方法:Thread th = new Thread(new ParameterizedThreadStart(Down()));     th.Start("开始");

2.前台线程和后台线程的区别

一般默认都是前台线程,主线程也是前台线程。前台线程结束后,后台线程自动结束(没执行完也会结束)。

在th.Start() 之前设置是否是后台线程:th.IsBackgroud = true;

3.互斥锁lock

让代码段同步执行。排队执行。

原文地址:https://www.cnblogs.com/hpbkin/p/7844869.html