zoukankan      html  css  js  c++  java
  • 一个简单的C#多线程间同步的例子.[转]

    在开发中经常会遇到线程的例子,如果某个后台操作比较费时间,我们就可以启动一个线程去执行那个费时的操作,同时程序继续执行。在某些情况下可能会出现多个线程的同步协同的问题,下面的例子就展示了在两个线程之间如何协同工作。

    这个程序的思路是共同做一件事情(从一个ArrayList中删除元素),如果执行完成了,两个线程都停止执行。

    代码如下:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;

    /// <summary>
    /// 在开发中经常会遇到线程的例子,如果某个后台操作比较费时间,我们就可以启动一个线程去执行那个费时的操作,同时程序继续执行。在某些情况下可能会出现多个线程的同步协同的问题,下面的例子就展示了在两个线程之间如何协同工作。
    ///
    ///这个程序的思路是共同做一件事情(从一个ArrayList中删除元素),如果执行完成了,两个线程都停止执行。
    ///作者:周公
    /// 时间:2008-5-17
    /// 原发地址:http://blog.csdn.net/zhoufoxcn
    /// </summary>
    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(
    thisnew EventArgs());//引发完成事件
                }
                Monitor.Exit(
    this);//取消锁定
                Thread.Sleep(5);
            }
        }

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

    说明:要实现线程同步不止这一种方式。在这里采用了事件,在事件处理程序里中止了线程(主要是回答csdn论坛的一个朋友的问题才用了这种办法)。

  • 相关阅读:
    Mysql:为什么用limit时,offset很大会影响性能
    [解决方案]未能找到路径“~in oslyncsc.exe”的一部分
    [经验分享]NuGet发布自己的Dll(类库包)
    [解决方案]使用百度富文本编辑器,编辑显示不了内容
    [解决方案]未能加载文件或程序集
    [经验分享]WebApi+SwaggerUI 完美展示接口
    [经验分享]Linux网络连接-VMware+CentOS 7
    [经验分享]WebAPI中返回类型JsonMessage的应用
    [解决方案]WebAPI+SwaggerUI部署服务器后,访问一直报错的问题
    [解决方案] 当 IDENTITY_INSERT 设置为 OFF 时
  • 原文地址:https://www.cnblogs.com/Godblessyou/p/2376313.html
Copyright © 2011-2022 走看看