c#多线程
1. Thread类
C#多线程编程中Thread类需要包含名称空间System.Threading。
class Program
{
static void Main(string[] args)
{
Thread thread01 = new Thread(ThreadTask01);
thread01.Start();
// thread01.Join();
for (int i = 0; i < 5; i++)
{
console.WriteLine("Thread Main is working !");
Thread.Sleep(20);
}
Console.ReadKey();
}
static void ThreadTask01()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Thread task one is working !");
Thread.Sleep(20);
}
}
}
输出:
Start不会阻塞主线程,如果加入Join,则会阻塞主线程的执行,直到子线程执行完成才把控制权交还给主流程,但是各个子线程之间的并行执行不受Join影响。上例中加入Join的输出:
多个线程同时访问一个公共字段时,容易引起线程安全问题,C#中使用关键字lock保证数据的安全性和同步:
class Program
{
//static readonly object locker = new object();
static int TotalNum = 100;
static void Main(string[] args)
{
Thread thread01 = new Thread(ThreadTask01);
Thread thread02 = new Thread(ThreadTask02);
thread01.Start();
thread02.Start();
Console.ReadKey();
}
static void ThreadTask01()
{
while (TotalNum > 0)
{
Console.WriteLine(TotalNum);
TotalNum--;
Thread.Sleep(10);
}
}
static void ThreadTask02()
{
while (TotalNum > 0)
{
Console.WriteLine(TotalNum);
TotalNum--;
Thread.Sleep(10);
}
}
}
输出异常,中间有重复输出的数字:
添加lock:
class Program
{
static readonly object locker = new object();
static int TotalNum = 100;
static void Main(string[] args)
{
Thread thread01 = new Thread(ThreadTask01);
Thread thread02 = new Thread(ThreadTask02);
thread01.Start();
thread02.Start();
Console.ReadKey();
}
static void ThreadTask01()
{
while (TotalNum > 0)
{
lock (locker)
{
Console.WriteLine(TotalNum);
TotalNum--;
}
Thread.Sleep(10);
}
}
static void ThreadTask02()
{
while (TotalNum > 0)
{
lock (locker)
{
Console.WriteLine(TotalNum);
TotalNum--;
}
Thread.Sleep(10);
}
}
}
输出正常:
2. parameterThreadStart委托
ParameterThreadStart委托定义为 void ParameterizedThreadStart(Object state)。
class Program
{
static int totalNum = 10;
static void Main(string[] args)
{
ParameterizedThreadStart thread01 = new ParameterizedThreadStart(ThreadTask01);
Thread thread = new Thread(thread01);
thread.Start(totalNum);
Console.ReadKey();
}
static void ThreadTask01(object obj)
{
for (int i = 0; i < Convert.ToInt32(obj); i++)
{
Console.WriteLine(i);
Thread.Sleep(10);
}
}
}
3. 匿名方法
匿名方法启动多线程是使用Thread类的另一种更加灵活的方法。
class Program
{
static void Main(string[] args)
{
ThreadStart threadStart = new ThreadStart(delegate ()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Thread one is working!");
}
});
Thread thread = new Thread(threadStart);
thread.Start();//多线程启动匿名方法
//等待线程结束
while (thread.ThreadState != ThreadState.Stopped)
{
Thread.Sleep(10);
}
Console.ReadKey();
}
}
4. Delegate委托
委托是一种比较高级的多线程方法,可以传入参数和获取返回值,还可以查询异步操作是否完成。
class Program
{
private delegate int NewTaskDelegate(int ms);
private static int newTask(int ms)
{
Console.WriteLine("任务开始");
Thread.Sleep(ms);
Random random = new Random();
int n = random.Next(10000);
Console.WriteLine("任务完成");
return n;
}
static void Main(string[] args)
{
NewTaskDelegate task = newTask;
IAsyncResult asyncResult = task.begininvoke(2000, null, null);
//EndInvoke方法将被阻塞2秒
//使用WaitOne阻塞主线程,直到子线程完成操作
while (!asyncResult.AsyncWaithandle.WaitOne(100, false))
{
Console.Write("-");
}
while (!asyncResult.IsCompleted) //查询线程是否完成
{
Console.Write("*");
Thread.Sleep(100);
}
int result = task.EndInvoke(asyncResult);
Console.WriteLine(result);
Console.Read();
}
}
文章最后发布于: 2016-12-30 23:53:46
相关阅读
去掉桌面图标的小箭头的步骤:一、下载魔方大师,然后解压,打开魔方大师。二、在魔方大师下面,有个美化大师,点击进入美化大师。三、在美
1。问题由来(这个程序得到的结果是0) package com.haut.thread; /** * @author shuiyu_lei * @date 2017/12/16 */ public clas
上篇我们一起聊了,界面常用的中文字体和英文字体,本期聊聊数字字体,数字在设计中其实占比还是很大,特别是在金融和电商设计中,数字字体
马尔可夫预测的性质及运用 对事件的全面预测,不仅要能够指出事件发生的各种可能结果,而且还必须给出每一种结果出现的概率,说明被预
iPhone上传照片到iCloud后本地保存的是一个缩略图,更换手机时通过iTools、爱思助手导出相册需要将这些照片下载原件到手机,一个图片