111,097
社区成员




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++;
}
}
}
public partial class Form1 : Form
{
private ThreadStart myStart;
private ParameterizedThreadStart myPStart;
int m, n, s = 0;
object t;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
t = s;
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 ((int)t % 2 != 0)
{
Monitor.Wait(t);
}
t =(int)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 ((int)t % 2 != num)
{
Monitor.Wait(t);
}
t =(int)t + num;
Monitor.Pulse(t);
}
finally
{
Monitor.Exit(t);
}
n++;
}
}
}
.try IL_001d to IL_0049
finally handler IL_0049 to IL_0051
.try IL_000e to IL_0055
finally handler IL_0055 to IL_0058
IL_003c: box [mscorlib]System.Int32
IL_0041: stfld object WindowsApplication1.Form1::t
IL_0046: nop
IL_0047: leave.s IL_0051
IL_0049: ldloc.1
IL_004a: call void [mscorlib]System.Threading.Monitor::Exit(object)
IL_004f: nop
IL_0050: endfinally
IL_0051: nop
IL_0052: nop
IL_0053: leave.s IL_0058
IL_0055: nop
IL_0056: nop
IL_0057: endfinally
IL_0058: nop
IL_0046: nop
IL_0047: leave.s IL_0051
IL_0049: ldloc.1
IL_004a: call void [mscorlib]System.Threading.Monitor::Exit(object)
IL_004f: nop
IL_0050: endfinally
IL_0051: nop
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.0
IL_0003: ldfld int32 WindowsApplication1.Form1::s
IL_0008: box [mscorlib]System.Int32
IL_000d: stfld object WindowsApplication1.Form1::t
,根据MSDN对装箱的定义,s被装箱后会被创建一个新的实例存储在t中,所以我觉得这里t是没有变的。 IL_0005: nop
IL_0006: ldc.i4.s 100
IL_0008: call void [mscorlib]System.Threading.Thread::Sleep(int32)
IL_000d: nop
IL_000e: nop
IL_000f: ldarg.0
IL_0010: ldfld object WindowsApplication1.Form1::t
IL_0015: dup
IL_0016: stloc.1
IL_0017: call void [mscorlib]System.Threading.Monitor::Enter(object)
IL_001c: nop
IL_0045: stloc.0
IL_0046: ldarg.0
IL_0047: ldloc.0
IL_0048: box [mscorlib]System.Int32
IL_004d: stfld object WindowsApplication1.Form1::t
IL_0052: nop
IL_0053: leave.s IL_0058
IL_0055: nop
IL_0056: nop
IL_0057: endfinally
IL_0048: box [mscorlib]System.Int32
IL_004d: stfld object WindowsApplication1.Form1::t
IL_0052: ldarg.0
IL_0053: ldfld object WindowsApplication1.Form1::t
IL_0058: call void [mscorlib]System.Threading.Monitor::Pulse(object)
IL_005d: nop
IL_005e: nop
IL_005f: leave.s IL_0070
IL_0061: nop
IL_0062: ldarg.0
IL_0063: ldfld object WindowsApplication1.Form1::t
IL_0068: call void [mscorlib]System.Threading.Monitor::Exit(object)
IL_006d: nop
IL_006e: nop
IL_006f: endfinally
IL_0070: nop
IL_0071: ldarg.0
IL_0072: dup
IL_0073: ldfld int32 WindowsApplication1.Form1::m
IL_0078: ldc.i4.1
IL_0079: add
IL_007a: stfld int32 WindowsApplication1.Form1::m