简单示例代码,多线程见协作
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");
}
}
}
}
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);
}
}
}
}
{
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);
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架