请看下面的代码
是关于线程间数据同步的一个测试
使当使用临界区时,很快完成。
使用信号灯时,完成之后又要等一大会线程才会退出,而且在等待过程中CPU占用资源很大。
是我写的代码错了,还是.net类库中的问题??using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.ProgressBar progressBar2;
int[] i ={ 0 };
int[] c ={ 0 };
AutoResetEvent are;
public MainForm()
{
InitializeComponent(); } [STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
} #region Windows Forms Designer generated code
private void InitializeComponent()
{
this.progressBar2 = new System.Windows.Forms.ProgressBar();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// progressBar2
//
this.progressBar2.Location = new System.Drawing.Point(72, 168);
this.progressBar2.Maximum = 10000;
this.progressBar2.Name = "progressBar2";
this.progressBar2.Size = new System.Drawing.Size(400, 23);
this.progressBar2.TabIndex = 1;
//
// progressBar1
//
this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.progressBar1.Location = new System.Drawing.Point(72, 56);
this.progressBar1.Maximum = 10000;
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(400, 23);
this.progressBar1.Step = 1;
this.progressBar1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 288);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 56);
this.button1.TabIndex = 3;
this.button1.Text = "全部";
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(192, 288);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(128, 56);
this.button2.TabIndex = 6;
this.button2.Text = "临界区";
this.button2.Click += new System.EventHandler(this.Button2Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(24, 64);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(17, 17);
this.label1.TabIndex = 4;
this.label1.Text = "CS";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(32, 172);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(17, 17);
this.label2.TabIndex = 5;
this.label2.Text = "SL";
//
// button3
//
this.button3.Location = new System.Drawing.Point(376, 288);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(128, 56);
this.button3.TabIndex = 7;
this.button3.Text = "信号灯";
this.button3.Click += new System.EventHandler(this.Button3Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(512, 349);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.progressBar2);
this.Controls.Add(this.progressBar1);
this.Name = "MainForm";
this.Text = "ThreadsTest";
this.ResumeLayout(false);
}
#endregion
void Button1Click(object sender, System.EventArgs e)
{
(new Thread(new ThreadStart(begin))).Start();
}
void begin()
{
this.Enabled = false;
Thread t1 = new Thread(new ThreadStart(CriticalSection));
Thread t2 = new Thread(new ThreadStart(SignalLamp));
t1.Start();
t2.Start();
t1.Join();
t2.Join();
this.Enabled = true;
}
void CriticalSection()
{
this.progressBar1.Value = 0;
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.Length; i++)
{
threads[i] = new Thread(new ThreadStart(CriticalSectionThread));
threads[i].Start();
}
for (int i = 0; i < threads.Length; i++)
{
threads[i].Join();
}
this.c[0] = 0;
}
void CriticalSectionThread()
{
while (true)
{
lock (c)
{
if (c[0] > 10000000)
{
break;
}
else
{
c[0]++;
if (c[0] % 1000 == 0)
{
progressBar1.PerformStep();
}
}
}
}
}
void SignalLamp()
{
this.progressBar2.Value = 0;
are = new AutoResetEvent(true);
Thread[] threads = new Thread[10];
for (int i = 0; i < 10; i++)
{
threads[i] = new Thread(new ThreadStart(SignalLampThread));
threads[i].Start();
}
for (int i = 0; i < threads.Length; i++)
{
threads[i].Join();
}
are.Close();
this.i[0] = 0;
}
void SignalLampThread()
{
while (true)
{
are.WaitOne();
if (i[0] > 10000000)
{
are.Set();
break;
}
else
{
i[0]++;
if (i[0] % 1000 == 0)
progressBar2.PerformStep();
are.Set();
}
}
}
void Button2Click(object sender, System.EventArgs e)
{
(new Thread(new ThreadStart(CriticalSectionTest))).Start();
}
void CriticalSectionTest()
{
this.Enabled = false;
Thread t1 = new Thread(new ThreadStart(CriticalSection));
t1.Start();
t1.Join();
this.Enabled = true;
}
void SignalLampTest()
{
this.Enabled = false;
Thread t1 = new Thread(new ThreadStart(SignalLamp));
t1.Start();
t1.Join();
this.Enabled = true;
}
void Button3Click(object sender, System.EventArgs e)
{
(new Thread(new ThreadStart(SignalLampTest))).Start();
}}
是关于线程间数据同步的一个测试
使当使用临界区时,很快完成。
使用信号灯时,完成之后又要等一大会线程才会退出,而且在等待过程中CPU占用资源很大。
是我写的代码错了,还是.net类库中的问题??using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.ProgressBar progressBar2;
int[] i ={ 0 };
int[] c ={ 0 };
AutoResetEvent are;
public MainForm()
{
InitializeComponent(); } [STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
} #region Windows Forms Designer generated code
private void InitializeComponent()
{
this.progressBar2 = new System.Windows.Forms.ProgressBar();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// progressBar2
//
this.progressBar2.Location = new System.Drawing.Point(72, 168);
this.progressBar2.Maximum = 10000;
this.progressBar2.Name = "progressBar2";
this.progressBar2.Size = new System.Drawing.Size(400, 23);
this.progressBar2.TabIndex = 1;
//
// progressBar1
//
this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.progressBar1.Location = new System.Drawing.Point(72, 56);
this.progressBar1.Maximum = 10000;
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(400, 23);
this.progressBar1.Step = 1;
this.progressBar1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 288);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 56);
this.button1.TabIndex = 3;
this.button1.Text = "全部";
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(192, 288);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(128, 56);
this.button2.TabIndex = 6;
this.button2.Text = "临界区";
this.button2.Click += new System.EventHandler(this.Button2Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(24, 64);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(17, 17);
this.label1.TabIndex = 4;
this.label1.Text = "CS";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(32, 172);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(17, 17);
this.label2.TabIndex = 5;
this.label2.Text = "SL";
//
// button3
//
this.button3.Location = new System.Drawing.Point(376, 288);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(128, 56);
this.button3.TabIndex = 7;
this.button3.Text = "信号灯";
this.button3.Click += new System.EventHandler(this.Button3Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(512, 349);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.progressBar2);
this.Controls.Add(this.progressBar1);
this.Name = "MainForm";
this.Text = "ThreadsTest";
this.ResumeLayout(false);
}
#endregion
void Button1Click(object sender, System.EventArgs e)
{
(new Thread(new ThreadStart(begin))).Start();
}
void begin()
{
this.Enabled = false;
Thread t1 = new Thread(new ThreadStart(CriticalSection));
Thread t2 = new Thread(new ThreadStart(SignalLamp));
t1.Start();
t2.Start();
t1.Join();
t2.Join();
this.Enabled = true;
}
void CriticalSection()
{
this.progressBar1.Value = 0;
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.Length; i++)
{
threads[i] = new Thread(new ThreadStart(CriticalSectionThread));
threads[i].Start();
}
for (int i = 0; i < threads.Length; i++)
{
threads[i].Join();
}
this.c[0] = 0;
}
void CriticalSectionThread()
{
while (true)
{
lock (c)
{
if (c[0] > 10000000)
{
break;
}
else
{
c[0]++;
if (c[0] % 1000 == 0)
{
progressBar1.PerformStep();
}
}
}
}
}
void SignalLamp()
{
this.progressBar2.Value = 0;
are = new AutoResetEvent(true);
Thread[] threads = new Thread[10];
for (int i = 0; i < 10; i++)
{
threads[i] = new Thread(new ThreadStart(SignalLampThread));
threads[i].Start();
}
for (int i = 0; i < threads.Length; i++)
{
threads[i].Join();
}
are.Close();
this.i[0] = 0;
}
void SignalLampThread()
{
while (true)
{
are.WaitOne();
if (i[0] > 10000000)
{
are.Set();
break;
}
else
{
i[0]++;
if (i[0] % 1000 == 0)
progressBar2.PerformStep();
are.Set();
}
}
}
void Button2Click(object sender, System.EventArgs e)
{
(new Thread(new ThreadStart(CriticalSectionTest))).Start();
}
void CriticalSectionTest()
{
this.Enabled = false;
Thread t1 = new Thread(new ThreadStart(CriticalSection));
t1.Start();
t1.Join();
this.Enabled = true;
}
void SignalLampTest()
{
this.Enabled = false;
Thread t1 = new Thread(new ThreadStart(SignalLamp));
t1.Start();
t1.Join();
this.Enabled = true;
}
void Button3Click(object sender, System.EventArgs e)
{
(new Thread(new ThreadStart(SignalLampTest))).Start();
}}
解决方案 »
- WPF XML 数据 绑定问题
- 怎么找不到类实例化后的方法
- 一正则表达式
- 请问用C#写的程序可以和用VC写的程序一起打包吗?
- WinForm窗体的MouseDown事件和MouseMove事件的问题
- C#的Excel编程问题!
- 有重谢!各位音频处理高手请进!开发批量调音频音量程序
- asp.net中使用 Bitmap.LockBits(Rectangle rect,ImageLockMode flags,PixelFormat format)出现参数无效?
- 生成PDF 关于大表格
- C# 类 传值
- 谁给个调用 api :FindWindow 和 ShowWindow的例子给我一个,谢谢!!
- C#做的群发邮件软件,线程自动调用,在服务器上发了几百封后就没反应了,请问高手是何原因?急!!!!!
void Button2Click(object sender, System.EventArgs e)
{
(new Thread(new ThreadStart(CriticalSectionTest))).Start();
}