码迷,mamicode.com
首页 > 编程语言 > 详细

多线程(一)初步使用

时间:2018-10-27 17:43:58      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:ogr   start   ati   i++   bubuko   turn   ret   call   col   

 class Program6
    {
        public static void ThreadProc()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("ThreadProc: {0}", i);
                // Yield the rest of the time slice.
                Thread.Sleep(0);
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Main thread: Start a second thread.");
            Thread t = new Thread(new ThreadStart(ThreadProc));
            t.Start();//子线程开始执行
            for (int i = 0; i < 4; i++)
            {
               
                Console.WriteLine("Main thread: Do some work.");
               // Console.WriteLine("Wait 1 second.");
                 Thread.Sleep(0);
            }
            Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
           
            t.Join(); //加这句话的意思是:先把子线程执行完,再执行本线程。
            Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
            Console.ReadLine();
        }
    }

结果:

技术分享图片

多线程(一)初步使用

标签:ogr   start   ati   i++   bubuko   turn   ret   call   col   

原文地址:https://www.cnblogs.com/25miao/p/9862363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!