代码改变世界

简单示例代码,多线程见协作

2011-10-11 15:39  chris-shao  阅读(259)  评论(0编辑  收藏  举报
一个简单的多线程示例,主要目的就是给一个账户加钱和给账户减钱,当账户余额小于一个值的时候,减钱的线程必须停止减钱,等有足够钱时,才能继续减钱。
using System;
using System.Collections.Generic;
using System.Text;
/*
 多线程演示实例
 
*/
namespace MultThreadExample
{
    class Program
    {
        static Account account = new Account();
        static void Main(string[] args)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(add));
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(subtract));
            Console.ReadLine();
        }
        static void add(object obj)
        {
            while (true)
            {
                account.Add();
                System.Threading.Thread.Sleep(1000);
            }
        }
        static void subtract(object obj)
        {
            while (true)
            {
                account.subtract();
                System.Threading.Thread.Sleep(100);
            }
        }
    }
    class Account
    {
        private object m_lock=new object();
        private int _count=0;
        public void Add()
        {
            lock (m_lock)
            {

                _count = _count + 1;
                Console.WriteLine("count:" + _count + "\t Add");
                System.Threading.Monitor.PulseAll(m_lock);

            }
        }
        public void subtract()
        {
            lock (m_lock)
            {

                while (_count <= 5)
                {
                    System.Threading.Monitor.Wait(m_lock);
                }
                _count--;
                Console.WriteLine("count:" + _count + "\t subtract");
            }

        }
    }
}

稍做修改,生产者消费者问题。

 

namespace ConsoleApplication1
{
    class Program
    {
        static Account account = new Account();
        static void Main(string[] args)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(add));
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(subtract));
            Console.ReadLine();
        }
        static void add(object obj)
        {
            while (true)
            {
                account.Add();
                System.Threading.Thread.Sleep(2000);
            }
        }
        static void subtract(object obj)
        {
            while (true)
            {
                account.subtract();
                System.Threading.Thread.Sleep(8000);
            }
        }
    }
    class Account
    {
        private object m_lock=new object();
        private System.Collections.ArrayList _ary = new System.Collections.ArrayList();
        public void Add()
        {
            lock (m_lock)
            {
                while (_ary.Count >= 5)
                {
                    //队列满
                    Console.WriteLine("队列满,等待消费:" );
                    System.Threading.Monitor.Wait(m_lock);
                }
                _ary.Add("product");
                Console.WriteLine("生产者 增加: 队列长度:" + _ary.Count);
                System.Threading.Monitor.Pulse(m_lock);
                
            }
        }
        public void subtract()
        {
            lock (m_lock)
            {

                while (_ary.Count <= 0)
                {
                    Console.WriteLine("队列空,等待生产:");
                    System.Threading.Monitor.Wait(m_lock);
                }
                _ary.RemoveAt(0);
                Console.WriteLine("消费者 减少: 队列长度:" + _ary.Count);
                System.Threading.Monitor.Pulse(m_lock);
            }

        }
    }
}