首页 > 学院 > 开发设计 > 正文

多线程简单实例

2019-11-14 16:42:38
字体:
来源:转载
供稿:网友
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();        }    }

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表