日期:2014-05-18 浏览次数:21109 次
public partial class Form1 : Form { private ThreadStart myStart; private ParameterizedThreadStart myPStart; int m, n, t; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { myStart = new ThreadStart(ShowNamea); Thread thA = new Thread(myStart); myPStart = new ParameterizedThreadStart(ShowNameObj); Thread thB = new Thread(myPStart); thA.Start(); thB.Start(2); } private void ShowNamea() { while(true) { Thread.Sleep(100); try { Monitor.Enter(t); if (t % 2 != 0) { Monitor.Wait(t); } t += 2; Monitor.Pulse(t); } finally { Monitor.Exit(t); } m++; } } private void ShowNameObj(object obj) { int num = (int)obj; while (true) { Thread.Sleep(100); try { Monitor.Enter(t); if (t % 2 != num) { Monitor.Wait(t); } t += num; Monitor.Pulse(t); } finally { Monitor.Exit(t); } n++; } } }