consumer.cs

来自「北大青鸟内部资料」· CS 代码 · 共 46 行

CS
46
字号
using System;
using System.Threading;

namespace Multithreading
{
	/// <summary>
	/// Summary description for Consumer.
	/// </summary>
	class Consumer
	{
		private SharedIntegerSynchronized sharedLocation;
		
		// constructor
		public Consumer( 
			SharedIntegerSynchronized shared)
		{
			sharedLocation = shared;
			
		}

		// read sharedLocation's value four times
		public void Consume()
		{
			int sum = 0;

			// get current thread
			Thread current = Thread.CurrentThread;

			// sleep for an interval of upto 400 milliseconds
			// then add sharedLocation's Buffer property value
			// to sum
			for ( int count = 1; count <= 4; count++ )
			{
				Thread.Sleep( 400 );
				sum += sharedLocation.Buffer;
			}
      
			Console.WriteLine( Thread.CurrentThread.Name + 
				" 读取值总计:" + sum + 
				".\n终止" + Thread.CurrentThread.Name + ".\n" );

		} // end method Consume

	} // end class Consumer
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?