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章 

猜你喜欢

转载自blog.csdn.net/xiaochenXIHUA/article/details/83444936