C#線程的使用和測試


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

namespace TestThread
{
    class Program
    {
        static void Main(string[] args)
        {
            //實例化三個線程對象
            Thread one = new Thread(func1);
            Thread two = new Thread(func2);
            Thread three = new Thread(func3);
            //對三個線程實例命名
            one.Name = "myThreadone";
            two.Name = "myThreadtwo";
            three.Name = "myThreadthree";

            //分別啓動三個線程
            one.Start();
            two.Start();
            three.Start();
            //讓線程休眠5秒鐘
            Thread.Sleep(500);

            //one.Suspend();    //讓線程one掛起
            three.Abort();     //線程終止
            two.Join();        //線程調用方法
            //one.Resume();      //恢復執行one線程

            Console.ReadKey();
            
        }

        //線程要調用的函數
        public static void func1()
        {
            Console.WriteLine("我是線程一輸出的結果。");
        }
        public static void func2()
        {
            Console.WriteLine("我是線程2輸出的結果。");
        }
        public static void func3()
        {
            Console.WriteLine("我是線程3輸出的結果。");
        }
    }
}

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