⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testmultipleworkitems.cs

📁 线程池实例,1.1版本,用于代替.net自带线程池
💻 CS
字号:
using System;
using System.Diagnostics;
using System.Threading;

using NUnit.Framework;

using Amib.Threading;

namespace SmartThreadPoolTests
{
	/// <summary>
	/// Summary description for MultipleWorkItemsExample.
	/// </summary>
	[TestFixture]
	[Category("TestMultipleWorkItems")]
	public class TestMultipleWorkItems
	{
		/// <summary>
		/// Example of how to queue several work items and then wait infinitely for 
		/// all of them to complete.
		/// </summary>
		[Test]
		public void WaitAll() 
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			bool success = true;

			IWorkItemResult [] wirs = new IWorkItemResult[5];

			for(int i = 0; i < wirs.Length; ++i)
			{
				wirs[i] = 
					smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
			}

			SmartThreadPool.WaitAll(wirs);

			for(int i = 0; i < wirs.Length; ++i)
			{
				if (!wirs[i].IsCompleted)
				{
					success = false;
					break;
				}
				else
				{
					int result = (int)wirs[i].GetResult();
					if (1 != result)
					{
						success = false;
						break;
					}
				}
			}

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		} 

		/// <summary>
		/// Example of how to queue several work items and then wait infinitely for 
		/// one of them to complete.
		/// 
		/// You can use this technique if you have several work items that return the same 
		/// infomration, but use different method to aquire it. Just execute all of them at
		/// once and wait for the first work item to complete.
		/// 
		/// For example: You need an information about a person and you can query several 
		/// information sites (FBI, CIA, etc.). Query all of them at once and use the first
		/// answer to arrive.
		/// </summary>
		[Test]
		public void WaitAny() 
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			bool success = false;

			IWorkItemResult [] wirs = new IWorkItemResult[5];

			for(int i = 0; i < wirs.Length; ++i)
			{
				wirs[i] = 
					smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
			}

			int index = SmartThreadPool.WaitAny(wirs);

			if (wirs[index].IsCompleted)
			{
				int result = (int)wirs[index].GetResult();
				if (1 == result)
				{
					success = true;
				}
			}

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		} 

		/// <summary>
		/// Example of how to queue several work items and then wait on a timeout for all
		/// of them to complete.
		/// </summary>
		[Test]
		public void WaitAllWithTimeoutSuccess()
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			bool success = true;

			IWorkItemResult [] wirs = new IWorkItemResult[5];

			for(int i = 0; i < wirs.Length; ++i)
			{
				wirs[i] = 
					smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
			}

			bool timeout = !SmartThreadPool.WaitAll(wirs, 1500, true);
			success = !timeout;

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		} 

		/// <summary>
		/// Example of how to queue several work items and then wait on a timeout for all
		/// of them to complete.
		/// </summary>
		[Test]
		public void WaitAllWithTimeoutFailure()
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			bool success = true;

			IWorkItemResult [] wirs = new IWorkItemResult[5];

			for(int i = 0; i < wirs.Length; ++i)
			{
				wirs[i] = 
					smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
			}

			bool timeout = !SmartThreadPool.WaitAll(wirs, 10, true);
			success = timeout;

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		} 

		/// <summary>
		/// Example of how to queue several work items and then wait on a timeout for any
		/// of them to complete.
		/// </summary>
		[Test]
		public void WaitAnyWithTimeoutSuccess()
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			bool success = true;

			IWorkItemResult [] wirs = new IWorkItemResult[5];

			for(int i = 0; i < wirs.Length; ++i)
			{
				wirs[i] = 
					smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
			}

			int index = SmartThreadPool.WaitAny(wirs, 1500, true);

			success = (index != WaitHandle.WaitTimeout);

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		}

		/// <summary>
		/// Example of how to queue several work items and then wait on a timeout for any
		/// of them to complete.
		/// </summary>
		[Test]
		public void WaitAnyWithTimeoutFailure()
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			bool success = true;

			IWorkItemResult [] wirs = new IWorkItemResult[5];

			for(int i = 0; i < wirs.Length; ++i)
			{
				wirs[i] = 
					smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
			}

			int index = SmartThreadPool.WaitAny(wirs, 10, true);

			success = (index == WaitHandle.WaitTimeout);

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		}

		[Test]
		public void WaitAllWithEmptyArray()
		{ 

			IWorkItemResult [] wirs = new IWorkItemResult[0];

			bool success = SmartThreadPool.WaitAll(wirs);;

			Assert.IsTrue(success);
		}

		private object DoSomeWork(object state)
		{ 
			Thread.Sleep(1000);
			return 1;
		}
	}
}

⌨️ 快捷键说明

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