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

📄 testmonitor.cs

📁 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的
💻 CS
📖 第 1 页 / 共 2 页
字号:
	}	public class MonitorInterruptDuringSleep	{		String result = "Oops - something went wrong!";		public String testMonitorInterruptDuringSleep()		{			Thread thread;			thread = new Thread(new ThreadStart(threadFunc));			thread.Start();			while ((thread.ThreadState & ThreadState.WaitSleepJoin) == 0)				continue;			thread.Interrupt();			thread.Join();			return this.result;		}		void threadFunc()		{			try			{				Thread.Sleep(1000 * 1000);				this.result = "Expected System.Threading.ThreadInterruptedException";				return;			}			catch (ThreadInterruptedException)			{			}			this.result = null;		}	}	/*	 * Test that an Interrupt outside of a lock hits the next attempt to lock.	 */	public void TestMonitorInterruptEnter()	{		if (!TestThread.IsThreadingSupported)		{			return;		}		MonitorInterruptEnter test = new MonitorInterruptEnter();		String result = test.testMonitorInterruptEnter();		if (result != null)	  		Assert(result, result == null);	}	public class MonitorInterruptEnter	{		Object o = new Object();		bool seen = false;		String result = "Oops - something went wrong!";		public String testMonitorInterruptEnter()		{			Thread thread;			lock (this)			{				thread = new Thread(new ThreadStart(threadFunc));				thread.Start();				Monitor.Wait(this);			}			thread.Interrupt();			lock (this.o)			{				this.seen = true;				Thread.MemoryBarrier();				thread.Join();			}			return this.result;		}		void threadFunc()		{			try			{				Monitor.Enter(this.o);			}			catch (System.Exception e)			{				this.result = "Got unexpected exception during Enter: " + e;				return;			}			lock (this)			{				Monitor.Pulse(this);			}			try			{				Monitor.Exit(this.o);			}			catch (System.Exception e)			{				this.result = "Got unexpected exception during Exit: " + e;				return;			}			while (!this.seen)			{				Thread.MemoryBarrier();			}			try			{				Monitor.Enter(this.o);				this.result = "Expected System.Threading.ThreadInterruptedException";				return;			}			catch (ThreadInterruptedException)			{			}			this.result = null;		}	}	/*	 * Test that an Abort breaks a Monitor.Enter wait.	 */	public void TestMonitorAbortEnter()	{		if (!TestThread.IsThreadingSupported)			return;		MonitorAbortEnter test = new MonitorAbortEnter();		String result = test.testMonitorAbortEnter();		if (result != null)	  		Assert(result, result == null);	}	public class MonitorAbortEnter	{		Object o = new Object();		bool seen = false;		String result = "Oops - something went wrong!";		public String testMonitorAbortEnter()		{			Thread thread;			lock (this)			{				thread = new Thread(new ThreadStart(threadFunc));				thread.Start();				Monitor.Wait(this);			}			lock (this.o)			{				this.seen = true;				Thread.MemoryBarrier();				Thread.Sleep(800);				thread.Abort();				thread.Join();			}			return this.result;		}		void threadFunc()		{			try			{				Monitor.Enter(this.o);			}			catch (System.Exception e)			{				this.result = "Got unexpected exception during Enter: " + e;				return;			}			try			{				lock (this)				{					try 					{						Monitor.Pulse(this);					}					catch (System.Exception e)					{						this.result = "Pulse throw exception: " + e;						return;					}				}			}			catch (System.Exception e)			{				this.result = "lock throw exception: " + e;				return;			}			try			{				Monitor.Exit(this.o);			}			catch (System.Exception e)			{				this.result = "Got unexpected exception during Exit: " + e;				return;			}			while (!this.seen)			{				Thread.MemoryBarrier();			}			try 			{				this.result = "Monitor.Enter threw an exception we couldn't catch!";				Monitor.Enter(this.o);				this.result = "Expected System.Threading.ThreadAbortException";				return;			}			catch (ThreadAbortException)			{				this.result = "Oops - something went wrong after catching ThreadAbortException";				if (!this.seen)				{					this.result = "Wait did not re-aquire lock after abort";					return;				}				try				{					System.Threading.Monitor.Exit(this.o);					this.result = "Incorrectly gained monitor on abort while entering";					return;				}				catch (SynchronizationLockException)				{					this.result = null;				}			}			catch (System.Exception e)			{				this.result = "Monitor.Enter threw wrong exception: " + e;				return;			}			this.result = "Abort was not automatically re-thrown in catch";		}	}	/*	 * Test that an Interrupt outside of a Sleep hits the next	 * attempt to Sleep.	 */	public void TestMonitorInterruptSleep()	{		if (!TestThread.IsThreadingSupported)		{			return;		}		MonitorInterruptSleep test = new MonitorInterruptSleep();		String result = test.testMonitorInterruptSleep();		if (result != null)	  		Assert(result, result == null);	}	public class MonitorInterruptSleep	{		Object o = new Object();		bool seen = false;		String result = "Oops - something went wrong!";		public String testMonitorInterruptSleep()		{			Thread thread;			lock (this)			{				thread = new Thread(new ThreadStart(threadFunc));				thread.Start();				Monitor.Wait(this);			}			thread.Interrupt();			this.seen = true;			Thread.MemoryBarrier();			thread.Join();			return this.result;		}		void threadFunc()		{			lock (this)			{				Monitor.Pulse(this);			}			while (!this.seen)			{				Thread.MemoryBarrier();			}			try			{				Thread.Sleep(1000 * 1000);				this.result = "Expected System.Threading.ThreadInterruptedException";				return;			}			catch (ThreadInterruptedException)			{			}			this.result = null;		}	}	/*	 * Test that a Monitor re-aquires the lock if aborted during a	 * Wait().	 */	public void TestMonitorAbortDuringWait()	{		if (!TestThread.IsThreadingSupported)		{			return;		}		MonitorAbortDuringWait test = new MonitorAbortDuringWait();		String result = test.testMonitorAbortDuringWait();		if (result != null)	  		Assert(result, result == null);	}	public class MonitorAbortDuringWait	{		Object o = new Object();		bool seen;		String result = "Oops - something went wrong!";		public String testMonitorAbortDuringWait()		{			Thread thread;			lock (this)			{				thread = new Thread(new ThreadStart(threadFunc));				thread.Start();				this.seen = false;				Monitor.Wait(this);			}			lock (this.o)			{				thread.Abort();				Thread.Sleep(800);				this.seen = true;			}			thread.Join();			return result;		}		void threadFunc()		{			Monitor.Enter(this.o);			try			{				lock (this)				{					try					{						Monitor.Pulse(this);					}					catch (System.Exception e)					{						this.result = "Pulse threw exception: " + e;						return;					}				}			}			catch (System.Exception e)			{				this.result = "lock threw exception: " + e;				return;			}			try 			{				this.result = "Monitor.Wait threw an exception we couldn't catch!";				Monitor.Wait(this.o);				this.result = "Expected System.Threading.ThreadAbortException";				return;			}			catch (ThreadAbortException)			{				this.result = "Oops - something went wrong after catching ThreadAbortException";				if (!this.seen)				{					this.result = "Wait did not re-aquire lock after abort";					return;				}				try				{					System.Threading.Monitor.Exit(this.o);					this.result = null;				}				catch (System.Exception e)				{					this.result = "Got unexpected exception during Exit: " + e;					return;				}			}			catch (System.Exception e)			{				this.result = "Monitor.Wait threw wrong exception: " + e;				return;			}			this.result = "Abort was not automatically re-thrown in catch";		}	}	/*	 * Test that a thread can not be interrupted re-aquiring the monitor	 * after a Wait().	 */	public void TestMonitorInterruptAfterWait()	{		if (!TestThread.IsThreadingSupported)			return;		MonitorInterruptAfterWait test = new MonitorInterruptAfterWait();		String result = test.testMonitorInterruptAfterWait();		if (result != null)	  		Assert(result, result == null);	}	public class MonitorInterruptAfterWait	{		Object o = new Object();		bool seen;		String result = "Oops - something went wrong!";		public String testMonitorInterruptAfterWait()		{			Thread thread;			lock (this)			{				thread = new Thread(new ThreadStart(threadFunc));				thread.Start();				this.seen = false;				Monitor.Wait(this);			}			lock (this.o)			{				Monitor.Pulse(o);				Thread.Sleep(800);				thread.Interrupt();				Thread.Sleep(800);				this.seen = true;			}			thread.Join();			return this.result;		}		void threadFunc()		{			Monitor.Enter(this.o);			lock (this)			{				Monitor.Pulse(this);			}			try			{				Monitor.Wait(this.o);				this.result = "Expected System.Threading.ThreadInterruptedException";				return;			}			catch (ThreadInterruptedException)			{			}			if (!this.seen)			{				this.result = "Wait did not re-aquire lock after interrupt";				return;			}			try			{				Monitor.Exit(this.o);			}			catch (System.Exception e)			{				this.result = "Got unexpected exception during Exit: " + e;				return;			}			this.result = null;		}	}	/*	 * Test that a thread can not be aborted re-aquiring the monitor	 * after a Wait().	 */	public void TestMonitorAbortAfterWait()	{		if (!TestThread.IsThreadingSupported)			return;		MonitorAbortAfterWait test = new MonitorAbortAfterWait();		String result = test.testMonitorAbortAfterWait();		if (result != null)	  		Assert(result, result == null);	}	public class MonitorAbortAfterWait	{		Object o = new Object();		bool seen;		String result = "Oops - something went wrong!";		public String testMonitorAbortAfterWait()		{			Thread thread;			lock (this)			{				thread = new Thread(new ThreadStart(threadFunc));				thread.Start();				this.seen = false;				Monitor.Wait(this);			}			lock (this.o)			{				Monitor.Pulse(o);				Thread.Sleep(800);				thread.Abort();				Thread.Sleep(800);				this.seen = true;			}			thread.Join();			return this.result;		}		void threadFunc()		{			Monitor.Enter(this.o);			try			{				lock (this)				{					try					{						Monitor.Pulse(this);					}					catch (System.Exception e)					{						this.result = "Pulse threw an exception: " + e;						return;					}				}			}			catch (System.Exception e)			{				this.result = "lock threw an exception: " + e;				return;			}			try 			{				this.result = "Monitor.Wait threw an exception we couldn't catch!";				Monitor.Wait(this.o);				this.result = "Expected System.Threading.ThreadAbortException";				return;			}			catch (ThreadAbortException)			{				this.result = "Oops - something went wrong after catching ThreadAbortException";				if (!this.seen)				{					this.result = "Wait did not re-aquire lock after abort";				Thread.Sleep(800);					return;				}				try				{					System.Threading.Monitor.Exit(this.o);					this.result = "finally not executed";				}				catch (System.Exception e)				{					this.result = "Got unexpected exception during Exit: " + e;					return;				}				finally				{					if (this.result == "finally not executed")					{										this.result = null;					}				}			}			catch (System.Exception e)			{				this.result = "Monitor.Wait threw wrong exception: " + e;				return;			}			this.result = "Abort was not automatically re-thrown in catch";		}	}}

⌨️ 快捷键说明

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