线程的同步和线程间的协调笔记
线程的同步和线程间的协调学习
线程的同步和线程间的协调
通常非同步线程的运行时间不可予知;
保护数据,当多个线程访问数据时,避免出现不可知的结果;
协调线程使线程在到达某个状态是可以改变其状态。
线程同步,线程安全
线程通常不能协调,需要开发者处理
可使用的同步对象:Interlocked、AutoResetEvant、ManualResetEvent、Monitor、Mutex
多线程访问对象可能会引发异常;
读取到不一致数据;
两个线程可能会同时更新数据导致错误结果
使用同步对象创建线程安全类;
只有需要并发的类才需要线程安全。
代码:
#define RUN_IN_SYNC //予定义运行时同步 不同步时该为#define NOT_RUN_IN_SYNC
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication2
{
public class Form1 : Form
{
//定义label显示字符
private string text;
private bool thread1Running;
private bool thread2Running;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
#if RUN_IN_SYNC
private Mutex myMutex;//用于进程间同步
#endif
public Form1()
{
InitializeComponent();
}
//程序住入口
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
//按钮
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "";
text = "";
button1.Enabled = false;
#if RUN_IN_SYNC
myMutex = new Mutex();
#endif
Thread workerThread1 = new Thread(new ThreadStart(Thread1Function));
Thread workerThread2 = new Thread(new ThreadStart(Thread2Function));
thread1Running = true;
thread2Running = true;
workerThread1.Start();
workerThread2.Start();
}
//线程执行函数1
private void Thread1Function()
{
#if RUN_IN_SYNC
myMutex.WaitOne();
#endif
for (int i = 0; i < 5; i++)
{
text += i.ToString();
Thread.Sleep(0);
}
#if RUN_IN_SYNC
myMutex.ReleaseMutex();
#endif
thread1Running = false;
this.Invoke(new EventHandler(WorkerThreadsFinished));
}
//线程执行函数2
private void Thread2Function()
{
#if RUN_IN_SYNC
myMutex.WaitOne();
#endif
for (int i = 0; i < 5; i++)
{
text += i.ToString();
Thread.Sleep(0);
}
#if RUN_IN_SYNC
myMutex.ReleaseMutex();
#endif
thread2Running = false;
this.Invoke(new EventHandler(WorkerThreadsFinished));
}
public delegate void EventHandler();
private void WorkerThreadsFinished()
{
label1.Text = text;
button1.Enabled = true;
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 176);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(72, 72);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(160, 23);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
}
}