2021-7-28 多线程的基本练习
多线程

using System; using System.Threading; namespace ThreadTest { class Program { static void Main(string[] args) { Thread th = Thread.CurrentThread; th.Name = "主线程"; Console.WriteLine(th.Name); Thread child = new Thread(ChildThread); child.Start(); Console.WriteLine("子线程正在休眠中");//当前线程继续执行 ThreadStart childref = new ThreadStart(NumberThread); Thread number = new Thread(childref); number.Start(); Thread.Sleep(2000); //number.Abort();//线程终止,netcore3.1不能使用终止线程 number.Interrupt();//中断线程 Thread.Sleep(2000); Console.WriteLine(number.ThreadState);//获取中断线程状态 Console.ReadKey(); } static void ChildThread() { var sleepTime = 5000; Console.WriteLine("子线程1休眠" + sleepTime / 1000 + "秒"); Thread.Sleep(sleepTime);//线程休眠5秒 Console.WriteLine("子线程1休息完毕"); } static void NumberThread() { try { for (int i = 0; i < 10; i++) { Thread.Sleep(500); Console.WriteLine(i); } } catch (ThreadInterruptedException e) { Console.WriteLine("线程已被中止"); } finally { Console.WriteLine("未找到问题"); } } } }
练习2

using System; using System.Threading; namespace MulitThread { class Program { static void Main(string[] args) { Book book = new Book(); Thread bookSale = new Thread(new ParameterizedThreadStart(book.BookSale)); Thread bookSale1 = new Thread(new ParameterizedThreadStart(book.BookSale)); bookSale.Start("小明"); bookSale1.Start("小红"); Console.ReadKey(); } } class Book { private int book = 5; public void BookSale(object name) { while (true) { string buyName = name as string; Thread.Sleep(500); int a = book; lock (this)//如果没加线程锁,线程可能会出现数据互蹿 { if (a > 0) { book--; Console.WriteLine($"{buyName}购买一本,书籍还剩{book}本"); } } } } } }
优化

using System; using System.Threading; namespace MulitThread { class Program { static void Main(string[] args) { Book book = new Book(); Thread bookSale = new Thread(new ParameterizedThreadStart(book.BookSale)); Thread bookSale1 = new Thread(new ParameterizedThreadStart(book.BookSale)); bookSale.Start("小明"); bookSale1.Start("小红"); Console.ReadKey(); } } class Book { private int book = 5; public void BookSale(object name) { while (true) { string buyName = name as string; Thread.Sleep(500); int a = book; lock (this)//如果没加线程锁,线程可能会出现数据互蹿 { if (a > 0) { book--; Console.WriteLine($"{buyName}购买一本,书籍还剩{book}本"); } else { break; } } } Console.WriteLine("书籍已经销售完了"); } } }
再次优化,当线程thread.sleep注释后出现-1所以可改成以下情况

using System; using System.Threading; namespace MulitThread { class Program { static void Main(string[] args) { Book book = new Book(); Thread bookSale = new Thread(new ParameterizedThreadStart(book.BookSale)); Thread bookSale1 = new Thread(new ParameterizedThreadStart(book.BookSale)); bookSale.Start("小明"); bookSale1.Start("小红"); Console.ReadKey(); } } class Book { private int book = 5; public void BookSale(object name) { while (true) { string buyName = name as string; Thread.Sleep(500); int a = book; lock (this)//如果没加线程锁,线程可能会出现数据互蹿 { if (a > 0) { book--; Console.WriteLine($"{buyName}购买一本,书籍还剩{book}本"); } else { break; } } } Console.WriteLine("书籍已经销售完了"); } } }