线程(Thread)

作者:追风剑情 发布于:2014-9-1 23:23 分类:C#

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

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号