C#線程切換

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        public static void print1()
        {
            for (int i = 0; i < 9; ++i)
            {
                Console.WriteLine(2 * i);
                Thread.Sleep(100);
            }
        }

        public static void print2()
        {
            for (int i = 0; i < 9; ++i)
            {
                Console.WriteLine(2 * i + 1);
                Thread.Sleep(100);
            }
        }

        static void Main(string[] args)
        {
            Thread t1 = new Thread(print1);
            Thread t2 = new Thread(print2);
            t1.Start();
            t2.Start();
            while (true) ;
        }
    }

}

執行程序,依次輸出0-16

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章