class PRogram { Thread t1;//线程1 Thread t2;//线程2 Thread t3;//线程3 int i = 0; static void Main(string[] args) { Program demo = new Program(); demo.GoStart(); } public Program() { t1 = new Thread(new ThreadStart(run)); t2 = new Thread(new ThreadStart(run)); t3 = new Thread(new ThreadStart(run)); t1.Name = "线程⑴"; t2.Name = "线程⑵"; t3.Name = "线程⑶"; } private void run() { while (1 == 1) { Monitor.Enter(this); Console.WriteLine(Thread.CurrentThread.Name+"输出了"+i); i++; if (i > 1000) { ThreadStop(); } Monitor.Exit(this); Thread.Sleep(10); } } private void ThreadStop() { Console.WriteLine("执行完成"); t1.Abort(); t2.Abort(); t3.Abort(); GC.Collect();//不知道是否有用,请大牛指点,本人连菜鸟都不是哦 } public void GoStart() { t1.Start(); t2.Start(); t3.Start(); } }
新闻热点
疑难解答