概念:
单线程程序:如果有多个任务,只能依次执行。当且仅当一个线程执行完后才执行下一个线程。
多线程程序:如果有多个任务,可以同时执行。
进程:指正在运行的程序。确切的来说,当一个程序进入内存运行那么这个程序变成了一个可执行的进程,进程是处于运行过程中的程序,并且具有一定独立性。
线程:是进程中的一个执行单元,负责当前进程中程序的执行,一个进程中至少有一个线程。
线程的生命周期:
创建、就绪、运行、阻塞、死亡。除创建和死亡不可逆之外其它三个阶段都可逆。
1,分时调度
所有线程轮流使用CPU的使用权,平均分配每个线程占用CPU的时间。
2,抢占式调度
优先让优先级高的线程使用CPU,如果线程的优先级相同,那么会随机选择一个(线程随机性)
简单的实例:
static void Main(string[] args) { //创建线程 Thread thread1 = new Thread(one); Thread thread2 = new Thread(two); //执行线程 thread1.Start(); thread2.Start(); Console.ReadKey(); } static void one() { for (int i = 0; i < 100; i++) { Console.WriteLine("one-->"+i); } } static void two() { for (int i = 0; i < 100; i++) { Console.WriteLine("two-->" + i); } }运行结果:
由此可见,此线程的执行方式也是抢占式执行。
线程同步需要给CPU上一把“锁”,在多线程开始执行的那一刻是抢占式的,一旦某个线程抢到了执行权都会在这个线程执行完毕后释放CPU“锁”供别的线程使用。
同步锁的两种方式,lock和monitor。lock用来锁线程中的某一段代码,monitor可以锁整个对象。如果你用lock框住整个线程中的代码,作用与monitor大同小异。
lock的使用:
class Program { //创建一把锁 static Object obj = new object(); static void Main(string[] args) { //创建线程 Thread thread1 = new Thread(one); Thread thread2 = new Thread(two); //执行线程 thread1.Start(); thread2.Start(); Console.ReadKey(); } static void one() { lock (obj) { for (int i = 0; i < 100; i++) { Console.WriteLine("one-->" + i); } }; } static void two() { lock (obj) { for (int i = 0; i < 100; i++) { Console.WriteLine("two-->" + i); } }; } }运行结果
monitor的使用:
class Program { //创建一把锁 static Object obj = new Object(); //开启创建线程 static Thread t3 = new Thread(three); static Thread t4 = new Thread(four); static void Main(string[] args) { //执行线程 t3.Start(); t4.Start(); Console.ReadKey(); } static void three() { Monitor.Enter(obj); for (int i = 0; i < 100; i++) { Console.WriteLine("three-->" + i); } Monitor.Exit(obj); } static void four() { Monitor.Enter(obj); for (int i = 0; i < 100; i++) { Console.WriteLine("four-->" + i); } Monitor.Exit(obj); } }运行结果:
以下为lock特有的作用(锁代码块):
class Program { //创建一把锁 static Object obj = new Object(); //开启创建线程 static Thread thread1 = new Thread(one); static Thread thread2 = new Thread(two); static void Main(string[] args) { //执行线程 thread1.Start(); thread2.Start(); Console.ReadKey(); } static void one() { for (int i = 0; i<100; i++) { Console.WriteLine("one-->" + i); } lock (obj) { for (int i = 0; i < 100; i++) { Console.WriteLine("one>" + i); } } } static void two() { for (int i = 0; i < 100; i++) { Console.WriteLine("tow-->" + i); } lock (obj) { for (int i = 0; i < 100; i++) { Console.WriteLine("two>" + i); } } } }运行的结果你会发现加了lock锁的代码块相对是同步的,没有lock锁的代码块为抢占式执行。
Monitor.Enter(obj);//获取锁
Monitor.Exit(obj);//释放锁
Monitor.Wait(obj);//将使用该锁的线程等待
Monitor.Pulse(obj);//将使用该锁的线程运行
thread1.IsBackground = true;//开启后台模式
thread1.Start();//开启线程
thread1.Abort();终止线程
thread2.Join();//在线程1中调用线程2的Join方法,会线程1执行前先执行线程2
Suspend和Resume方法不知在那个版本开始已过时
在lock中使用monitor:
class Program { //创建一把锁 static Object obj = new Object(); //开启创建线程 static Thread thread1 = new Thread(one); static Thread thread2 = new Thread(two); static void Main(string[] args) { //执行线程 thread1.Start(); thread2.Start(); Console.ReadKey(); } static void one() { lock (obj) { for (int i = 0; i < 100; i++) { Console.WriteLine("one>" + i); if (i == 50) { Monitor.Wait(obj); } } } } static void two() { lock (obj) { for (int i = 0; i < 100; i++) { Console.WriteLine("two>" + i); if (i == 20) { Thread.Sleep(2000); Monitor.Pulse(obj); } } } } }运行结果:
