请看下面的代码
是关于线程间数据同步的一个测试
使当使用临界区时,很快完成。
使用信号灯时,完成之后又要等一大会线程才会退出,而且在等待过程中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();
    }}

解决方案 »

  1.   

    请不要线程里再创建线程了,这样效率低!!//下面程序可以用异步
    void Button2Click(object sender, System.EventArgs e)
        {
            (new Thread(new ThreadStart(CriticalSectionTest))).Start();
        }
      

  2.   

    晕,线程没有休眠期,不占CPU才怪,
      

  3.   

    问题出现在Step=10 ,实际上你做了10000次PerformStep,那么最大值应该是10*10000,而你的progressBar设置的最大值为10000,所以当进度条不动时,线程还在运行,并没有停止所以还要等一回儿线程才结束。