日期:2014-05-17  浏览次数:20782 次

(多线程)在类内和类外:两种创建线程的方式执行过程有哪些不同?
请教各位前辈,用如下两种方式创建启动的线程,执行过程有哪些不同?谢谢各位

“方式一:使用ThreadA类创建3个线程”和“方式二:使用ThreadB类创建3个线程”


    /// <summary>
    /// 类ThreadA
    /// </summary>
    public class ThreadA
    {
        private Thread Th;
        private int number;

        public ThreadA(int n)
        {
            Th = new Thread(new ThreadStart(this.Run));
            this.number = n;
        }

        public void Start()
        {
            if (Th != null)
            {
                Th.Start();
            }
        }

        private void Run()
        {
            if (this.number > 0)
            {
                //线程执行方法代码,略……
            }
        }
    }

    /// <summary>
    /// 类ThreadB
    /// </summary>
    public class ThreadB
    {
        private int number;

        public ThreadB(int n)
        {
            this.number = n;
        }

        public void Run()
        {
            if (this.number > 0)
            {
                //线程执行方法代码,略……
            }
        }
    }


    class Program
    {
        static void Main(string[] args)
        {

            //方式一:使用ThreadA类创建3个线程
            ThreadA ThA1 =