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
线程池
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)
{
TestThreadMethod();
Console.ReadKey();
}
static void TestThreadMethod()//多线程
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Console.WriteLine($"当前线程ID:{Thread.CurrentThread.ManagedThreadId}");
for (int i = 0; i < 5; i++)
{
ThreadPool.QueueUserWorkItem(t => TestThread("线程池"));
//ThreadPool.QueueUserWorkItem(t => TestThread(t.ToString()), "线程池");
}
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
总耗时:2
当前线程ID:5,当前时间:2019-28-28 10:28:52:681
当前线程ID:3,当前时间:2019-28-28 10:28:52:681
当前线程ID:8,当前时间:2019-28-28 10:28:52:681
当前线程ID:6,当前时间:2019-28-28 10:28:52:681
当前线程ID:4,当前时间:2019-28-28 10:28:52:681
线程:线程池,结果:4999999950000000,当前线程ID:4,当前时间:2019-28-28 10:28:53:036
线程:线程池,结果:4999999950000000,当前线程ID:6,当前时间:2019-28-28 10:28:53:042
线程:线程池,结果:4999999950000000,当前线程ID:5,当前时间:2019-28-28 10:28:53:042
线程:线程池,结果:4999999950000000,当前线程ID:8,当前时间:2019-28-28 10:28:53:052
线程:线程池,结果:4999999950000000,当前线程ID:3,当前时间:2019-28-28 10:28:53:056
线程池线程等待
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)
{
TestThreadMethod();
Console.ReadKey();
}
static void TestThreadMethod()//多线程
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Console.WriteLine($"当前线程ID:{Thread.CurrentThread.ManagedThreadId}");
for (int i = 0; i < 5; i++)
{
using (ManualResetEvent m=new ManualResetEvent(false))
{
ThreadPool.QueueUserWorkItem(t => { TestThread("线程池");m.Set(); });
//ThreadPool.QueueUserWorkItem(t => TestThread(t.ToString()), "线程池");
m.WaitOne();
}
}
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:3,当前时间:2019-39-28 10:39:48:142
线程:线程池,结果:4999999950000000,当前线程ID:3,当前时间:2019-39-28 10:39:48:375
当前线程ID:4,当前时间:2019-39-28 10:39:48:375
线程:线程池,结果:4999999950000000,当前线程ID:4,当前时间:2019-39-28 10:39:48:655
当前线程ID:3,当前时间:2019-39-28 10:39:48:655
线程:线程池,结果:4999999950000000,当前线程ID:3,当前时间:2019-39-28 10:39:48:911
当前线程ID:3,当前时间:2019-39-28 10:39:48:911
线程:线程池,结果:4999999950000000,当前线程ID:3,当前时间:2019-39-28 10:39:49:158
当前线程ID:4,当前时间:2019-39-28 10:39:49:158
线程:线程池,结果:4999999950000000,当前线程ID:4,当前时间:2019-39-28 10:39:49:390
总耗时:1254
Task:基于线程池,API被强化。
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)
{
TestThreadMethod();
Console.ReadKey();
}
static void TestThreadMethod()//多线程
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Console.WriteLine($"当前线程ID:{Thread.CurrentThread.ManagedThreadId}");
List<Task> taskList = new List<Task>();
TaskFactory taskFactory = new TaskFactory();
for (int i = 0; i < 5; i++)
{
taskList.Add(taskFactory.StartNew(() => TestThread("Task")));
}
Task.WaitAll(taskList.ToArray());
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:5,当前时间:2019-48-28 10:48:13:136
当前线程ID:3,当前时间:2019-48-28 10:48:13:136
当前线程ID:4,当前时间:2019-48-28 10:48:13:136
当前线程ID:8,当前时间:2019-48-28 10:48:13:136
当前线程ID:7,当前时间:2019-48-28 10:48:13:136
线程:Task,结果:4999999950000000,当前线程ID:5,当前时间:2019-48-28 10:48:13:484
线程:Task,结果:4999999950000000,当前线程ID:3,当前时间:2019-48-28 10:48:13:486
线程:Task,结果:4999999950000000,当前线程ID:7,当前时间:2019-48-28 10:48:13:491
线程:Task,结果:4999999950000000,当前线程ID:8,当前时间:2019-48-28 10:48:13:492
线程:Task,结果:4999999950000000,当前线程ID:4,当前时间:2019-48-28 10:48:13:492
总耗时:439
ContinueWhenAll
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)
{
TestThreadMethod();
Console.ReadKey();
}
static void TestThreadMethod()//多线程
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Console.WriteLine($"当前线程ID:{Thread.CurrentThread.ManagedThreadId}");
List<Task> taskList = new List<Task>();
TaskFactory taskFactory = new TaskFactory();
for (int i = 0; i < 5; i++)
{
taskList.Add(taskFactory.StartNew(() => TestThread("Task")));
}
taskFactory.ContinueWhenAll(taskList.ToArray(), t => Console.WriteLine($"ContinueWhenAll_当前线程ID:{Thread.CurrentThread.ManagedThreadId}"));
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
总耗时:75
当前线程ID:4,当前时间:2019-53-28 10:53:32:055
当前线程ID:5,当前时间:2019-53-28 10:53:32:055
当前线程ID:3,当前时间:2019-53-28 10:53:32:054
当前线程ID:7,当前时间:2019-53-28 10:53:32:054
当前线程ID:6,当前时间:2019-53-28 10:53:32:058
线程:Task,结果:4999999950000000,当前线程ID:5,当前时间:2019-53-28 10:53:32:409
线程:Task,结果:4999999950000000,当前线程ID:4,当前时间:2019-53-28 10:53:32:433
线程:Task,结果:4999999950000000,当前线程ID:6,当前时间:2019-53-28 10:53:32:445
线程:Task,结果:4999999950000000,当前线程ID:7,当前时间:2019-53-28 10:53:32:459
线程:Task,结果:4999999950000000,当前线程ID:3,当前时间:2019-53-28 10:53:32:479
ContinueWhenAll_当前线程ID:3
ContinueWhenAny
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)
{
TestThreadMethod();
Console.ReadKey();
}
static void TestThreadMethod()//多线程
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Console.WriteLine($"当前线程ID:{Thread.CurrentThread.ManagedThreadId}");
List<Task> taskList = new List<Task>();
TaskFactory taskFactory = new TaskFactory();
for (int i = 0; i < 5; i++)
{
taskList.Add(taskFactory.StartNew(() => TestThread("Task")));
}
taskFactory.ContinueWhenAny(taskList.ToArray(), t => Console.WriteLine($"ContinueWhenAny_当前线程ID:{Thread.CurrentThread.ManagedThreadId}"));
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
总耗时:43
当前线程ID:4,当前时间:2019-56-28 10:56:35:891
当前线程ID:5,当前时间:2019-56-28 10:56:35:891
当前线程ID:3,当前时间:2019-56-28 10:56:35:885
当前线程ID:7,当前时间:2019-56-28 10:56:35:888
当前线程ID:6,当前时间:2019-56-28 10:56:35:919
线程:Task,结果:4999999950000000,当前线程ID:4,当前时间:2019-56-28 10:56:36:235
ContinueWhenAny_当前线程ID:10
线程:Task,结果:4999999950000000,当前线程ID:5,当前时间:2019-56-28 10:56:36:256
线程:Task,结果:4999999950000000,当前线程ID:3,当前时间:2019-56-28 10:56:36:259
线程:Task,结果:4999999950000000,当前线程ID:6,当前时间:2019-56-28 10:56:36:259
线程:Task,结果:4999999950000000,当前线程ID:7,当前时间:2019-56-28 10:56:36:265