C#实现多线程的几种方式

使用 Thread 类

    public static void ThreadMethod()
    {
        var newThread = new Thread(WorkerMethod);
        newThread.Start();
        for (int i = 0; i < 8; i++)
        {
            Console.WriteLine($"ThreadMethod 主线程开始工作:{i}");
            Thread.Sleep(100);
        }
    }
    private static void WorkerMethod()
    {
        for (int i = 0; i < 8; i++)
        {
            Console.WriteLine($"WorkerMethod 辅助线程开始工作:{i}");
            Thread.Sleep(100);
        }
    }

使用 ThreadPool 类

        public static void ThreadPoolMethod()
        {
            ThreadPool.QueueUserWorkItem(o => WorkerMethod());
            for (int i = 0; i < 8; i++)
            {
                Console.WriteLine($"ThreadPoolMethod 主线程开始工作:{i}");
                Thread.Sleep(100);
            }
        }
        private static void WorkerMethod()
        {
            for (int i = 0; i < 8; i++)
            {
                Console.WriteLine($"WorkerMethod 辅助线程开始工作:{i}");
                Thread.Sleep(100);
            }
        }

使用 Task 类

        public static void TaskMethod()
        {
            Task.Run(() => WorkerMethod());
            for (int i = 0; i < 8; i++)
            {
                Console.WriteLine($"TaskMethod 主线程开始工作:{i}");
                Task.Delay(100).Wait();
            }
        }
        private static void WorkerMethod()
        {
            for (int i = 0; i < 8; i++)
            {
                Console.WriteLine($"WorkerMethod 辅助线程开始工作:{i}");
                Thread.Sleep(100);
            }
        }

使用 Parallel 类

        public static void ParallelMethod()
        {
            Parallel.Invoke(WorkerMethod, WorkerMethodOther1, WorkerMethodOther2);
        }
        private static void WorkerMethod()
        {
            for (int i = 0; i < 8; i++)
            {
                Console.WriteLine($"WorkerMethod 辅助线程开始工作:{i}");
                Thread.Sleep(100);
            }
        }
        private static void WorkerMethodOther1()
        {
            for (int i = 0; i < 8; i++)
            {
                Console.WriteLine($"WorkerMethodOther1 辅助线程开始工作:{i}");
                Thread.Sleep(100);
            }
        }
        private static void WorkerMethodOther2()
        {
            for (int i = 0; i < 8; i++)
            {
                Console.WriteLine($"WorkerMethodOther2 辅助线程开始工作:{i}");
                Thread.Sleep(100);
            }
        }

Comments

No comments yet. Why don’t you start the discussion?

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注