using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace thread
{
    class thread_run
    {
        public void run1()
        {
            Console.WriteLine("run 1");
        }
        public void run2()
        {
            Console.WriteLine("run 2");
        }
        public void run3()
        {
            Console.WriteLine("run 3");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            thread_run tr = new thread_run();
            Thread th1 = new Thread(new ThreadStart(tr.run1));
            th1.Priority = ThreadPriority.Normal;
            th1.Start();
            Thread th2 = new Thread(new ThreadStart(tr.run2));
            th2.Priority = ThreadPriority.Highest;
            th2.Start();
            Thread th3 = new Thread(new ThreadStart(tr.run3));
            th3.Priority = ThreadPriority.Lowest;
            th3.Start();
            Console.Read();
        }
    }
}
vs2005
编译运行按钮
 出现输出是:
run2
run1
run3打开debug文件夹下面的编译出的exe
出现输出
run1
run2
run3用release 编译运行或者点文件直接运行结果都是
run2
run1
run3用shapdevelop
点三角运行结果
run2
run1
run3
点叹号运行
run1
run2
run3问题:
1,为什么会出现不同结果?
2,我比较倾向出现
run1
run2
run3
这个答案,但是另外的结果我就不明白了,程序不应该是顺序执行的吗?三个线程启动时间不同怎么会出现线程抢占呢?
新人发帖,请高手解答,,,

解决方案 »

  1.   

    线程的执行顺序是不定的,与当时的环境和cpu调度有关
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;namespace thread
    {
        class thread_run
        {
            public void run1()
            {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine("run 1");
                }
            }
            public void run2()
            {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine("run 2");
                }
            }
            public void run3()
            {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine("run 3");
                }
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                thread_run tr = new thread_run();
                Thread th1 = new Thread(new ThreadStart(tr.run1));
                th1.Priority = ThreadPriority.Normal;
                
                Thread th2 = new Thread(new ThreadStart(tr.run2));
                th2.Priority = ThreadPriority.Highest;
                
                Thread th3 = new Thread(new ThreadStart(tr.run3));
                th3.Priority = ThreadPriority.Lowest;
                
                
                
                th1.Start();
                th2.Start();
                th3.Start();//《--
                Console.Read();
            }
        }
    }看这个程序,这样运行结果是一样的,但是如果吧有注释的那一行放到th1.Start();上面结果就不一样了,就是th2先出了
      

  3.   

    线程不加控制时 完成情况没法精确预计,因为 线程之间存在CPU片断处理上的交替
      

  4.   

    CLR会自动根据当时的运行环境调度线程,所以设置的线程优先级,会得到不同的结果。
      

  5.   

    问题自己解决,我觉得里面还藏着一个线程 ,就是主线程,级别默认normal 所以是四个线程抢占问题,这样想,所有问题就对了