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

📄 step1.cs

📁 这五个程序都经实验
💻 CS
字号:
/// <disclaimer>
/// This software is protected by your own conscience (c).
/// You can use it however you want and wherever you want.
/// You are allowed to change this code, copy this code, or delete this code.
/// You can buy this code; sell this code; present this code to your mom on her birthday.
/// You can replace author抯 name with your own. You also can replace all the code with your own leaving
/// only author抯 name.
/// The only thing you cannot do, is to violate this license agreement. You simply are not able to.
/// </disclaimer>
/// <author>
/// Sergei Zotin 
/// szotin@shaw.ca
/// Burnaby, BC, Canada
/// Feel free to contact me for bug reports, feature requests and contract offers.
/// </author>
/// <version>1.6</version>

using System;
using System.Threading;

class A
{
	private B b;
	private int x;
	private int y;

	public B TheB
	{
		get
		{
			lock ( this )
			{
				return b;
			}
		}
		set
		{
			lock ( this )
			{
				b = value;
			}
		}
	}

	public int X
	{
		get
		{
			lock ( this )
			{
				return x;
			}
		}
		set
		{
			lock ( this )
			{
				Thread.Sleep ( 10 ); // just to increase the odds of a deadlock
				x = value;
				b.X = value;
			}
		}
	}

	public int Y
	{
		get
		{
			lock ( this )
			{
				return y;
			}
		}
		set
		{
			lock ( this )
			{
				Thread.Sleep ( 10 ); // just to increase the odds of a deadlock
				y = value;
			}
		}
	}
}

class B
{
	private A a;
	private int x;
	private int y;

	public A TheA
	{
		get
		{
			lock ( this )
			{
				return a;
			}
		}
		set
		{
			lock ( this )
			{
				a = value;
			}
		}
	}

	public int X
	{
		get
		{
			lock ( this )
			{
				return x;
			}
		}
		set
		{
			lock ( this )
			{
				Thread.Sleep ( 10 ); // just to increase the odds of a deadlock
				x = value;
			}
		}
	}

	public int Y
	{
		get
		{
			lock ( this )
			{
				return y;
			}
		}
		set
		{
			lock ( this )
			{
				Thread.Sleep ( 10 ); // just to increase the odds of a deadlock
				y = value;
				a.Y = value;
			}
		}
	}
}

class Step1
{
	private A a;
	private B b;

	private Step1 ( A _a, B _b )
	{
		a = _a;
		b = _b;
		a.TheB = b;
		b.TheA = a;
	}

	private void RunX ()
	{
		for ( int i = 0; i < 20; i++ )
		{
			a.X = i;
			System.Console.WriteLine ( "Thread X: a.X={0}; b.X={1}", a.X, b.X );
		}
	}

	private void RunY ()
	{
		for ( int i = 0; i < 20; i++ )
		{
			b.Y = i;
			System.Console.WriteLine ( "Thread Y: a.Y={0}; b.Y={1}", a.Y, b.Y );
		}
	}

	[STAThread]
	static void Main ( string[] args )
	{
		A a = new A();
		B b = new B();
		Step1 step1 = new Step1 ( a, b );

		Thread threadX = new Thread ( new ThreadStart ( step1.RunX ) );
		Thread threadY = new Thread ( new ThreadStart ( step1.RunY ) );
		threadX.Start ();
		threadY.Start ();

		// Most likely you will never get here because of a deadlock
		threadX.Join ();
		threadY.Join ();

		System.Console.WriteLine ( "Press Enter..." );
		System.Console.In.Read();
		System.Environment.Exit ( 0 );
	}
}

⌨️ 快捷键说明

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