通过线程计数器和Thread.Join方法得到线程已完成状态

方法一:线程计数器


    class Program
    {
        static void Main(string[] args)
        {
            Thread[] ths = new Thread[3];

            ThreadCounter counter1 = new ThreadCounter(1000);
            ThreadCounter counter2 = new ThreadCounter(3000);
            ThreadCounter counter3 = new ThreadCounter(7000);

            Thread countera = new Thread(new ThreadStart(counter1.run));
            Thread counterb = new Thread(new ThreadStart(counter2.run));
            Thread counterc = new Thread(new ThreadStart(counter3.run));

            countera.Start();
            counterb.Start();
            counterc.Start();

            Console.ReadKey();
        }
    }

    class ThreadCounter
    {
        private static int count = 0;
        private int ms;
        private static void Increment()
        {
            lock (typeof(ThreadCounter))
            {
                count++;
            }
        }

        private static void Decrease()
        {
            lock (typeof(ThreadCounter))
            {
                count--;
            }
        }

        private static int getCount()
        {
            lock (typeof(ThreadCounter))
            {
                return count;
            }
        }

        public ThreadCounter(int ms)
        {
            Increment();
            this.ms = ms;
        }

        public void run()
        {
            Thread.Sleep(ms);
            Console.WriteLine(ms.ToString() + "毫秒任务结束");
            Decrease();
            if (getCount() == 0)
            {
                Console.WriteLine("-------所有任务结束-------");
            }
        }
    }

方法二:Thread.Join方法

    class Program
    {
        static void Main(string[] args)
        {
            ThreadJoin join1 = new ThreadJoin(1000);
            ThreadJoin join2 = new ThreadJoin(3000);
            ThreadJoin join3 = new ThreadJoin(7000);
            Thread th1 = new Thread(new ThreadStart(join1.run));
            Thread th2 = new Thread(new ThreadStart(join2.run));
            Thread th3 = new Thread(new ThreadStart(join3.run));

            ThreadJoin join = new ThreadJoin();
            join.Join(new Thread[] { th1, th2, th3 });

            Console.ReadKey();
        }
    }

    class ThreadJoin
    {
        private int ms;
        public ThreadJoin()
        {

        }

        public ThreadJoin(int ms)
        {
            this.ms = ms;
        }

        public void run()
        {
            Thread.Sleep(ms);
            Console.WriteLine(ms + "毫秒任务结束");
        }

        public void Join(object[] obj)
        {
            Thread[] ths = obj as Thread[];
            foreach (Thread th in ths)
            {
                th.Start();
                th.Join();
            }
            Console.WriteLine("所有线程结束");
        }
    }

转载自:http://developer.51cto.com/art/200908/147794.htm 自己做了修改

推荐:http://www.laikanxia.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值