C#生产者与消费者问题(二)

简介:
/*
 * Created by SharpDevelop.
 * User: huangyibiao
 * Date: 2013/8/27
 * Time: 11:12
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Threading;

namespace ThreadTest
{
	class Program
	{
		public static void Main(string[] args)
		{
			// create share resource 
			Resource sharedResource = new Resource();
			
			ProduceThread produceThread = new ProduceThread(sharedResource);
			ConsumeThread consumeThread = new ConsumeThread(sharedResource);
			
			// create two thread
			Thread producer = new Thread(new ThreadStart(produceThread.Run));
			Thread consumer = new Thread(new ThreadStart(consumeThread.Run));
			
			try
			{
				producer.Start();
				consumer.Start();
				
				producer.Join();
				consumer.Join();
			}
			catch (ThreadStateException e)
			{
				Console.WriteLine(e.ToString());
			}
			
			Console.ReadKey();
		}
		
	}
	
	public class ProduceThread
	{
		/// <summary>
		/// share resource
		/// </summary>
		Resource _resource;
		
		public ProduceThread(Resource res)
		{
			_resource = res;
		}
		
		// begin to produce
		public void Run()
		{
			for (int i = 0; i < 10; ++i)
			{
				_resource.Produce();
			}
		}
	}
	
	public class ConsumeThread
	{
		/// <summary>
		/// share resource
		/// </summary>
		Resource _resource;
		
		public ConsumeThread(Resource res)
		{
			_resource = res;
		}
		
		public void Run()
		{
			for (int i = 0; i < 10; ++i)
			{
				_resource.Consume();
			}
		}
	}
	
	public class Resource
	{
		int _resourceContent = 0;
		bool _flag = false;
		
		/// <summary>
		/// consume method
		/// </summary>
		public void Consume()
		{
			// get access power
			Monitor.Enter(this);
			
			// if this object has been locked by other thread, then wait.
			if (!_flag)
			{
				try
				{
					Monitor.Wait(this);
				}
				catch (SynchronizationLockException e)
				{
					Console.WriteLine(e);
				}
				catch (ThreadInterruptedException e)
				{
					Console.WriteLine(e);
				}
			}
			
			// show contents after getting the access power
			Console.WriteLine("Consumer: {0}", _resourceContent);
			_flag = false;
			
			Monitor.Pulse(this);// tell the waiting thread, Consume method has finished
			Monitor.Exit(this); // exit the synchronization 
		}
		
		/// <summary>
		/// puroduce method
		/// </summary>
		public void Produce()
		{
			// get access power
			Monitor.Enter(this);
			
			// if this object has been locked by other thread, then wait.
			if (_flag)
			{
				try
				{
					Monitor.Wait(this);
				}
				catch (SynchronizationLockException e)
				{
					Console.WriteLine(e);
				}
				catch (ThreadInterruptedException e)
				{
					Console.WriteLine(e);
				}
			}
			
			// show contents after getting the access power
			Console.WriteLine("Consumer: {0}", ++_resourceContent);
			_flag = true;
			
			Monitor.Pulse(this);// tell the waiting thread, Consume method has finished
			Monitor.Exit(this); // exit the synchronization 
		}
	}
}

目录
相关文章
|
1月前
|
C#
24. C# 编程:用户设定敌人初始血值的实现
24. C# 编程:用户设定敌人初始血值的实现
19 0
|
2月前
|
SQL 数据库连接 应用服务中间件
C#WinForm基础编程(三)
C#WinForm基础编程
73 0
|
2月前
C#WinForm基础编程(二)
C#WinForm基础编程
55 0
|
2月前
|
C# 数据安全/隐私保护
C#WinForm基础编程(一)
C#WinForm基础编程
60 0
|
4月前
|
数据采集 前端开发 C#
C#编程艺术:Fizzler库助您高效爬取www.twitter.com音频
Twitter是全球最大的社交媒体平台之一,包含丰富的音频资源。用户可以在Twitter上发布、转发、评论和收听各种音频内容,如音乐、播客、新闻、故事等,直接从Twitter抓取音频数据并非易事,尤其是在考虑到可能的封锁和反爬虫机制。Twitter会对频繁访问的IP地址进行限制或封禁,以防止恶意爬虫的行为。因此,我们需要使用一些技术手段来规避这些障碍,确保稳定而高效的数据访问。
C#编程艺术:Fizzler库助您高效爬取www.twitter.com音频
|
3月前
|
程序员 C#
深入理解 C# 编程:枚举、文件处理、异常处理和数字相加
枚举是一个特殊的“类”,表示一组常量(不可更改/只读变量)。 要创建枚举,请使用 enum 关键字(而不是 class 或 interface),并用逗号分隔枚举项:
38 0
|
2天前
|
存储 安全 网络安全
C#编程的安全性与加密技术
【4月更文挑战第21天】C#在.NET框架支持下,以其面向对象和高级特性成为安全软件开发的利器。本文探讨C#在安全加密领域的应用,包括使用System.Security.Cryptography库实现加密算法,利用SSL/TLS保障网络传输安全,进行身份验证,并强调编写安全代码的重要性。实际案例涵盖在线支付、企业应用和文件加密,展示了C#在应对安全挑战的同时,不断拓展其在该领域的潜力和未来前景。
|
2天前
|
程序员 C#
C#编程中的面向对象编程思想
【4月更文挑战第21天】本文探讨了C#中的面向对象编程,包括类、对象、封装、继承和多态。类是对象的抽象,定义属性和行为;对象是类的实例。封装隐藏内部细节,只暴露必要接口。继承允许类复用和扩展属性与行为,而多态使不同类的对象能通过相同接口调用方法。C#通过访问修饰符实现封装,使用虚方法和抽象方法实现多态。理解并应用这些概念,能提升代码的清晰度和可扩展性,助你成为更好的C#程序员。