线程独立性

    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[] { 100, 200, 300, 400, 500 };
            for (int i = 0; i < 5; i++) {
                // 线程运行时独立性的,所以参数不要共享
                int id = i;
                int time = array[i];
                // 线程带参数传递
                Thread myThread = new Thread(t => myFunction(id, time));
                myThread.Start();
            }
            Console.Read();
        }

        static void myFunction(int id, int time)
        {
            Console.WriteLine(id  + " started....");
            Thread.Sleep(time);
            Console.WriteLine(id + " finished....");
        }
    }

 参考https://www.cnblogs.com/a924453846/articles/5129235.html

posted @ 2015-06-24 16:10  江境纣州  阅读(19)  评论(0编辑  收藏  举报