C# 五十七、进程+线程+多线程+Thread类 (二)

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

namespace TestCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //TestMethod();
            TestThreadMethod();
            Console.ReadKey();
        }

        static void TestMethod()//单线程
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            Console.WriteLine($"当前线程ID:{Thread.CurrentThread.ManagedThreadId}");
            for (int i = 0; i < 5; i++)
            {
                TestThread("单线程");
            }
            stopwatch.Stop();
            Console.WriteLine($"总耗时:{stopwatch.ElapsedMilliseconds}");
        }

        static void TestThreadMethod()//多线程
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            Console.WriteLine($"当前线程ID:{Thread.CurrentThread.ManagedThreadId}");

            List<Thread> listThread = new List<Thread>();
            for (int i = 0; i < 5; i++)
            {
                Thread thread = new Thread(() => TestThread("多线程"));
                listThread.Add(thread);
                thread.Start();
            }
            listThread.ForEach(t=>t.Join());
            stopwatch.Stop();
            Console.WriteLine($"总耗时:{stopwatch.ElapsedMilliseconds}");
        }

        static void TestThread(string threadName)
        {
            Console.WriteLine($"当前线程ID:{Thread.CurrentThread.ManagedThreadId},当前时间:{DateTime.Now.ToString("yyyy-mm-dd hh:mm:ss:fff")}");
            long a = 0;
            for (int i = 0; i < 100000000; i++)
            {
                a += i;
            }
            Console.WriteLine($"线程:{threadName},结果:{a},当前线程ID:{Thread.CurrentThread.ManagedThreadId},当前时间:{DateTime.Now.ToString("yyyy-mm-dd hh:mm:ss:fff")}");
        }
    }
}

--->单线程输出:
当前线程ID:1
当前线程ID:1,当前时间:2019-15-28 10:15:23:770
线程:单线程,结果:4999999950000000,当前线程ID:1,当前时间:2019-15-28 10:15:24:007
当前线程ID:1,当前时间:2019-15-28 10:15:24:008
线程:单线程,结果:4999999950000000,当前线程ID:1,当前时间:2019-15-28 10:15:24:265
当前线程ID:1,当前时间:2019-15-28 10:15:24:265
线程:单线程,结果:4999999950000000,当前线程ID:1,当前时间:2019-15-28 10:15:24:541
当前线程ID:1,当前时间:2019-15-28 10:15:24:541
线程:单线程,结果:4999999950000000,当前线程ID:1,当前时间:2019-15-28 10:15:24:774
当前线程ID:1,当前时间:2019-15-28 10:15:24:774
线程:单线程,结果:4999999950000000,当前线程ID:1,当前时间:2019-15-28 10:15:25:019
总耗时:1251

--->多线程输出:
当前线程ID:1
当前线程ID:3,当前时间:2019-15-28 10:15:55:289
当前线程ID:6,当前时间:2019-15-28 10:15:55:293
当前线程ID:4,当前时间:2019-15-28 10:15:55:289
当前线程ID:7,当前时间:2019-15-28 10:15:55:317
当前线程ID:5,当前时间:2019-15-28 10:15:55:288
线程:多线程,结果:4999999950000000,当前线程ID:4,当前时间:2019-15-28 10:15:55:632
线程:多线程,结果:4999999950000000,当前线程ID:3,当前时间:2019-15-28 10:15:55:638
线程:多线程,结果:4999999950000000,当前线程ID:5,当前时间:2019-15-28 10:15:55:647
线程:多线程,结果:4999999950000000,当前线程ID:7,当前时间:2019-15-28 10:15:55:670
线程:多线程,结果:4999999950000000,当前线程ID:6,当前时间:2019-15-28 10:15:55:676
总耗时:425

线程池

猜你喜欢

转载自blog.csdn.net/NCZ9_/article/details/88862556