c# 多線程的幾種方式

1.什麼是線程?html

進程做爲操做系統執行程序的基本單位,擁有應用程序的資源,進程包含線程,進程的資源被線程共享,線程不擁有資源。多線程


2.前臺線程和後臺線程的區別?異步

程序關閉時,後臺線程直接關閉,但前臺線程會執行完後關閉。spa

經過Thread類新建線程默認爲前臺線程。其餘方式建立的都是後臺線程。操作系統


多線程的幾種方式線程

一. 異步多線程code

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);
            for(int i = 0; i< 5;i++)
            {
                Action<string> action = t =>
                {
                    for(int k = 0; k< 1000000000;k++)
                    { }
                    Console.WriteLine("當前參數是{0},當前線程是{1}",t,Thread.CurrentThread.ManagedThreadId);
                };

                action.BeginInvoke("參數是" + i, null, null);
            }
            Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);
            Console.Read();
        }
    }

二. Threads線程htm

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);
            for(int i = 0; i< 5;i++)
            {
                ParameterizedThreadStart threadStart = t =>
                {
                    for (int k = 0; k < 1000000000; k++)
                    { }
                    Console.WriteLine("當前參數是{0},當前線程是{1}", t, Thread.CurrentThread.ManagedThreadId);
                };

                Thread thread = new Thread(threadStart);

                thread.Start(i);
            }
            Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }

三. ThreadPool線程池blog

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);
            for(int i = 0; i< 5;i++)
            {
                WaitCallback waitCallback = t =>
                {
                    for (int k = 0; k < 1000000000; k++)
                    { }
                    Console.WriteLine("當前參數是{0},當前線程是{1}", t, Thread.CurrentThread.ManagedThreadId);
                };

                ThreadPool.QueueUserWorkItem(waitCallback, i);
            }
            Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }

四. Task進程

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            TaskFactory taskFactory = new TaskFactory();

            for (int i = 0; i< 5;i++)
            {
                Action<object> action = t =>
                {
                    for (int k = 0; k < 1000000000; k++)
                    { }
                    Console.WriteLine("當前參數是{0},當前線程是{1}", t, Thread.CurrentThread.ManagedThreadId);
                };

                taskFactory.StartNew(action,i);
            }
            Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }

五. Parallel

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);


            Action action1 = () =>
            {
                for (int k = 0; k < 1000000000; k++)
                { }
                Console.WriteLine("當前參數是1,當前線程是{0}", Thread.CurrentThread.ManagedThreadId);
            };
            Action action2 = () =>
            {
                for (int k = 0; k < 1000000000; k++)
                { }
                Console.WriteLine("當前參數是2,當前線程是{0}", Thread.CurrentThread.ManagedThreadId);
            };
            Action action3 = () =>
            {
                for (int k = 0; k < 1000000000; k++)
                { }
                Console.WriteLine("當前參數是3,當前線程是{0}", Thread.CurrentThread.ManagedThreadId);
            };

            Parallel.Invoke(action1, action2, action3);

            Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }
上面簡單介紹了5種多線程的使用方式,後續文章會記錄每種多線程的詳細使用方法.
相關文章
相關標籤/搜索