一個簡單的C#多線程間同步的例子

來源:互聯網
上載者:User

 

{
this.style.display='none'; document.getElementById('Code_Closed_Text_220034').style.display='none'; document.getElementById('Code_Open_Image_220034').style.display='inline'; document.getElementById('Code_Open_Text_220034').style.display='inline';
}" id="Code_Closed_Image_220034">{
this.style.display='none'; document.getElementById('Code_Open_Text_220034').style.display='none'; getElementById('Code_Closed_Image_220034').style.display='inline'; getElementById('Code_Closed_Text_220034').style.display='inline';
}" id="Code_Open_Image_220034" style="display: none">Code
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;

public class ThreadDemo
{
    private Thread threadOne;
    private Thread threadTwo;
    private ArrayList stringList;
    private event EventHandler OnNumberClear;//資料刪除完成引發的事件
    public static void Main()
    {
        ThreadDemo demo = new ThreadDemo(1000);
        demo.Action();
    }
    public ThreadDemo(int number)
    {
        Random random = new Random(1000000);
        stringList = new ArrayList(number);
        for (int i = 0; i < number; i++)
        {
            stringList.Add(random.Next().ToString());
        }
        threadOne = new Thread(new ThreadStart(Run));//兩個線程共同做一件事情
        threadTwo = new Thread(new ThreadStart(Run));//兩個線程共同做一件事情
        threadOne.Name = "線程1";
        threadTwo.Name = "線程2";
        OnNumberClear += new EventHandler(ThreadDemo_OnNumberClear);
        
    }
    /// <summary>
    /// 開始工作
    /// </summary>
    public void Action()
    {
        threadOne.Start();
        threadTwo.Start();
    }
    /// <summary>
    /// 共同做的工作
    /// </summary>
    private void Run()
    {
        string stringValue = null;
        while (true)
        {
            Monitor.Enter(this);//鎖定,保持同步
            stringValue = (string)stringList[0];
            Console.WriteLine(Thread.CurrentThread.Name + "刪除了" + stringValue);
            stringList.RemoveAt(0);//刪除ArrayList中的元素
            if (stringList.Count == 0)
            {
                OnNumberClear(this, new EventArgs());//引發完成事件
            }
            Monitor.Exit(this);//取消鎖定
            Thread.Sleep(5);
        }
    }

    //執行完成之後,停止所有線程
    void ThreadDemo_OnNumberClear(object sender, EventArgs e)
    {
        Console.WriteLine("執行完了,停止了所有線程的執行。");
        threadTwo.Abort();
        threadOne.Abort();
        
    }
}

 

說明:要實現線程同步不止這一種方式。在這裡採用了事件,在事件處理常式裡中止了線程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.