Thread类的应用

来源:互联网 发布:stoll制版程序员招 编辑:程序博客网 时间:2024/05/17 05:05

Thread.join方法

join方法只有在线程结束时才继续执行下面的语句(会依次执行)。可以对每一个线程调用它的join方法,但要注意,这个调用要在另一个线程里,而不要在主线程,否则程序会被阻塞的。在调用join前先要Start开启线程

看一个阻塞主线程的方法

static void Main(string[] args)        {            int ms = 1000;            Thread thread1 = new Thread(Mymethod);            thread1.Name = "thread1";            Thread thread2 = new Thread(Mymethod);            thread2.Name = "thread2";            Thread thread3 = new Thread(Mymethod);            thread3.Name = "thread3";            for (int i = 0; i < 10; i++)            {                Console.WriteLine("主线程工作了 " + i.ToString() + " 次.");                if (i == 5)                {                    Thread[] tr = new Thread[] { thread1, thread2, thread3 };                    foreach (Thread t in tr)                    {                        t.Start(ms);                        t.Join();                    }                }            }                                Console.ReadKey();        } private static void Mymethod(object obj)        {            Thread.Sleep(Int32.Parse(obj.ToString()));            Console.WriteLine(Thread.CurrentThread.Name.ToString() + "结束任务用时:" + obj);        }        


执行效果如图

 

把Join不放在主线程中执行

 class Program
    {    

        static void Main(string[] args)
        {
            int ms = 1000;
            Thread thread1 = new Thread(Mymethod);
            thread1.Name = "thread1";
            Thread thread2 = new Thread(Mymethod);
            thread2.Name = "thread2";
            Thread thread3 = new Thread(Mymethod);
            thread3.Name = "thread3";
            thread1.Start(100);
            thread2.Start(2000);
            thread3.Start(3000);
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("主线程工作了 " + i.ToString() + " 次.");
                Thread.Sleep(1000);             
            }
            Thread joinThread = new Thread(JoinAllThread);
            joinThread.Start(new Thread[] { thread1, thread2, thread3 });


            Console.ReadKey();
        }

        private static void JoinAllThread(object obj)
        {
            Thread[] threads = obj as Thread[];
            foreach (Thread t in threads)
            {             
                t.Join();
            }
        }

        private static void Mymethod(object obj)
        {
            Thread.Sleep(Int32.Parse(obj.ToString()));
            Console.WriteLine(Thread.CurrentThread.Name.ToString() + "结束任务用时:" + obj);
        }
       
    }

执行效果如图

 

定义一个线程类
我们可以将Thread类封装在一个MyThread类中,以使任何从MyThread继承的类都具有多线程能力。MyThread类的代码如下

 class Program : MyThread
    {
        static void Main(string[] args)
        {
            Program nt = new Program();
            nt.start();
            Console.ReadKey();
        }

        override public void run()
        {
            Console.WriteLine("使用MyThread建立并运行线程");
        }
    }

    abstract class MyThread
    {
        Thread thread = null;

        abstract public void run();

        public void start()
        {
            if (thread == null)
                thread = new Thread(run);
            thread.Start();
        }
    }

 

 

 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 忘记了qq号绑定了手机怎么办 微信绑定qq号忘记了怎么办 扣扣密码忘了手机号换了怎么办 如何更改微信登录密码忘记了怎么办 阿里卖家不肯同意退货退款怎么办 新买的号码被绑定微信怎么办 如果换手机原来旧手机支付宝怎么办 支付宝注册证件已过有效期怎么办 支付宝账号是手机号换号怎么办 淘宝网卖假药伤了别人身体怎么办 微信手机登录收不到验证码怎么办 淘宝店的宝贝没有尺码选项怎么办 淘宝账号被保护要自助开通怎么办 小米平板经常出现系统无响应怎么办 淘宝买家被取消运费险了怎么办 新开的淘宝直播店没有流量怎么办 淘宝买的东西一直不发货怎么办 在淘宝上买了东西不发货怎么办 宝贝好几天拉一次吃的也少怎么办 淘宝快递正在运输途中想退货怎么办 淘宝买的东西退货商家拒收怎么办 手机屛上出现了微信图标怎么办 手机用了两年了反应太慢怎么办 魅族手机显示手机已锁定怎么办 为什么微信注册要安全验证码怎么办 微信帐号异常无法领取红包怎么办 帮朋友代付在支付宝被骗怎么办 商家说未收到货拒绝退款怎么办 淘宝商家拒绝退款怎么办还没收货的 拼多多点错确认收货了怎么办 被别人用菜刀砍伤没钱看病怎么办 东京下了订单但不发货怎么办 绑定卷皮钱包的手机号码丢了怎么办 小孩回奶在垫的被子上发霉了怎么办 2个月宝宝不喝母乳只喝奶瓶怎么办 我的扣扣被盗了朋友别被骗了怎么办 我买的股票退市了我的钱怎么办啊 在美食林被门口买宝石的骗了怎么办 在商场买的彪马鞋子皮子裂了怎么办 手机换号了京东钱包里的余额怎么办 寄报销发票给顺丰快递搞丢了怎么办