- 异步多线程
- 异步的执行顺序
- Thread
- 线程池
异步多线程
常见的同步方法:
using System;
namespace AsyncPractice
{
internal class AsyncClass
{
public AsyncClass()
{
Action action = () => this.DoSomething();
action.Invoke();
}
private void DoSomething()
{
Console.WriteLine("DoSomething");
}
}
}
常见的异步方法:
using System;
using System.Threading;
namespace AsyncPractice
{
internal class AsyncClass
{
public AsyncClass()
{
//异步方法
{
Action action = () => this.DoSomething();
action.BeginInvoke(null, null);
}
//异步方法 返回值为第二个参数
{
Action action = () => this.DoSomething();
var actionReturn = action.BeginInvoke(null, 1);
Console.WriteLine(actionReturn.AsyncState);
}
//CallBack
{
Action action = () => this.DoSomething();
AsyncCallback callbcak = t =>
{
Console.WriteLine(t.ToString());
Console.WriteLine("CallBack");
};
var actionReturn = action.BeginInvoke(callbcak, 2);
Console.WriteLine(actionReturn.AsyncState);
}
//等待异步结束
{
Action action = () => this.DoSomething();
AsyncCallback callbcak = t =>
{
Console.WriteLine(t.ToString());
Console.WriteLine("CallBack");
};
var actionReturn = action.BeginInvoke(callbcak, 2);
Console.WriteLine(actionReturn.AsyncState);
while (!actionReturn.IsCompleted)
{
Thread.Sleep(100);
Console.WriteLine("Async running...");
}
Console.WriteLine("wait");
}
}
private void DoSomething()
{
Thread.Sleep(1000);
Console.WriteLine("DoSomething");
}
}
}
异步的执行顺序
普通异步的执行顺序。
BeginInvoke返回值的执行顺序:
CallBack执行顺序:
emsp;带有阻塞的异步:
多线程
using System;
using System.Threading;
namespace ThreadPractice
{
internal class ThreadClass
{
public ThreadClass()
{
{
//无参多线程
Thread thread = new Thread(DoSomething);
thread.IsBackground = true;
thread.Start();
}
{
//有参多线程(ThreadStart)
for (int i = 0; i < 10; i++)
{
string name = string.Format("Thread{0}", i);
ThreadStart threadStart = () => this.DoSomething(name);
Thread thread = new Thread(threadStart);
thread.Start();
}
}
{
//有参多线程(ParameterizedThreadStart)
for (int i = 0; i < 10; i++)
{
string name = string.Format("Thread{0}", i);
ParameterizedThreadStart param = t => this.DoSomething(name);
Thread thread = new Thread(param);
thread.Start();
}
}
}
private void DoSomething()
{
Console.WriteLine("Thread Start 当前线程:{0},时间:{1},", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
long sum = 0;
for (int i = 1; i < 999999999; i++)
{
sum += i;
}
Console.WriteLine("TestThread End 当前线程:{0},时间:{1},i:{2}", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), sum);
}
private void DoSomething(string threadName)
{
Console.WriteLine("Thread Start Name={2},当前线程:{0},时间:{1},", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), threadName);
long sum = 0;
for (int i = 1; i < 999999999; i++)
{
sum += i;
}
Console.WriteLine("TestThread End Name={2},当前线程:{0},时间:{1},i:{3}", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), threadName, sum);
}
}
}
线程池
using System;
using System.Threading;
namespace ThreadPractice
{
internal class ThreadClass
{
public ThreadClass()
{
{
WaitCallback wait = t => this.DoSomething();
ThreadPool.QueueUserWorkItem(wait);
}
{
WaitCallback wait = t => this.DoSomething(t.ToString());
ThreadPool.QueueUserWorkItem(wait, "QueueName");
}
{
//阻塞
ManualResetEvent mre = new ManualResetEvent(false);
WaitCallback wait = t =>
{
mre.Set();
Console.WriteLine("{0}", t.ToString());
};
ThreadPool.QueueUserWorkItem(wait, "Param T");
Console.WriteLine("DoSomething...");
Console.WriteLine("DoSomething...");
Console.WriteLine("DoSomething...");
Console.WriteLine("DoSomething...");
mre.WaitOne();
}
}
private void DoSomething()
{
Console.WriteLine("Thread Start 当前线程:{0},时间:{1},", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
long sum = 0;
for (int i = 1; i < 999999999; i++)
{
sum += i;
}
Console.WriteLine("TestThread End 当前线程:{0},时间:{1},i:{2}", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), sum);
}
private void DoSomething(string threadName)
{
Console.WriteLine("Thread Start Name={2},当前线程:{0},时间:{1},", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), threadName);
long sum = 0;
for (int i = 1; i < 999999999; i++)
{
sum += i;
}
Console.WriteLine("TestThread End Name={2},当前线程:{0},时间:{1},i:{3}", System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), threadName, sum);
}
}
}