1. 程式人生 > >C#多執行緒基礎(多執行緒的優先順序、狀態、同步)

C#多執行緒基礎(多執行緒的優先順序、狀態、同步)

一、關於多執行緒的優先順序、狀態、同步指令碼如下:

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

namespace Test_NetWork_MultiThreading
{
    class Demo
    {
        static void Main(string[] args)
        {
            //例項化該類
            Demo obj = new Demo();

            ////多執行緒優先順序展示
            //obj.MutiThreadPriorityDisplay();

            ////多執行緒狀態展示
            //obj.MutiThreadStateDisplay();

            //多執行緒執行緒同步

            obj.ThreadAsync();


            Console.ReadLine();
        }

        #region    執行緒優先順序方法

        //多執行緒優先順序展示
        public void MutiThreadPriorityDisplay()
        {
            Thread th1 = new Thread(Thread1);                                   //定義執行緒
            Thread th2 = new Thread(Thread2);
            Thread th3 = new Thread(Thread3);

            th1.Priority = ThreadPriority.Highest;                              //指定執行緒的優先順序為最高
            th2.Priority = ThreadPriority.Normal;
            th3.Priority = ThreadPriority.Lowest;

            th1.Start();                                                        //啟動執行緒
            th2.Start();
            th3.Start();
        }

        //執行緒1
        private static void Thread1()
        {
            for (int i=0;i<1000;i++)
            {
                Console.WriteLine("執行緒1 "+"當前執行的次數為: "+i);
            }
        }

         //執行緒2
        private static void Thread2()
        {
            for (int i=0;i<1000;i++)
            {
                Console.WriteLine("執行緒2 "+"當前執行的次數為: "+i);
            }
        }

         //執行緒3
        private static void Thread3()
        {
            for (int i=0;i<1000;i++)
            {
                Console.WriteLine("執行緒3 "+"當前執行的次數為: "+i);
            }
        }
        #endregion


        #region 多執行緒狀態

        //多執行緒狀態展示
        public void MutiThreadStateDisplay()
        {
            Thread th1 = new Thread(Th_1);
            Thread th2 = new Thread(Th_2);
            th1.Start();
            th2.Start();
            Console.WriteLine();
        }

        private void Th_1()
        {
            for (int i=0;i<100;i++)
            {
                Console.WriteLine("我是執行緒1:"+i);
                if(i>=30)
                {
                    Thread.CurrentThread.Abort();
                }
            }
        }

        private void Th_2()
        {
            for (int i = 0; i < 2; i++)
            {
                string str = "你好,歡迎來到執行緒學習 \n";
                foreach (char c in str)
                {
                    Console.Write(c);
                    Thread.Sleep(1000);
                }
            }
        }

        #endregion


        #region   多執行緒執行緒同步(使用鎖)

        //執行緒同步
        public void ThreadAsync()
        {
            TestThreadClass ttc = new TestThreadClass();

            //Thread tA = new Thread(new ThreadStart(ttc.Add));
            Thread tA = new Thread((ttc.Add));
            tA.Name = "tA";
            tA.Start();

            //Thread tB = new Thread(new ThreadStart(ttc.Add));
            Thread tB = new Thread((ttc.Add));
            tB.Name = "tB";
            tB.Start();

        }


        //測試執行緒同步類
        public class TestThreadClass
        {
            private object obj = new object();
            private int num = 0;

            public void Add()
            {
                while (true)
                {
                    lock (obj)
                    {
                        num++;
                        Thread.Sleep(100);
                        Console.WriteLine(Thread.CurrentThread.Name + ":" + num);
                    }
                   

                }
            }
        }

        #endregion

    }//class_end
}

注:內容來自《Unity3D/2D遊戲開發從0到1》28章