日期:2014-05-17 浏览次数:21237 次
namespace ThreadDemo
{
public partial class Form1 : Form
{
private Yan y = new Yan();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(T1);
t1.Name = "t1";
Thread t2 = new Thread(T2);
t2.Name = "t2";
t1.Start();
t2.Start();
}
private void T1()
{
lock (y)
{
for (int i = 0; i < 10000000; ++i) ;
y.str = "先";
MessageBox.Show(y.str);
}
}
private void T2()
{
y.str = "后";
MessageBox.Show(y.str);
}
}
public class Yan
{
public string str = "";
}
}