鸟语天空
线程(Thread)
post by:追风剑情 2014-9-1 23:23

using System;
using System.Threading;

namespace ThreadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建两条线程a,b
            Thread a = new Thread(new ThreadStart(ThreadA));
            Thread b = new Thread(new ThreadStart(ThreadB));
            //启动线程a,b
            a.Start();
            b.Start();

            //等待a,b线程结束后再往下执行
            a.Join();
            b.Join();

            Console.WriteLine("\nMain End!");

            Console.ReadKey();
        }

        static void ThreadA()
        {
            for (int i = 0; i < 20; i++)
            {
                Console.Write("A");
                Thread.Sleep(1);//让出时间片
            }
        }

        static void ThreadB()
        {
            for (int i = 0; i < 50; i++)
            {
                Console.Write("B");
                Thread.Sleep(1);//让出时间片
            }
        }
    }
}

运行结果

Thread运行结果.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容