【发布时间】:2013-08-07 23:15:26
【问题描述】:
这是我的代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Writer {
public void Write(string xxx) { Console.Write(xxx); }
}
class Program
{
static Writer wrt;
static void Main(string[] args)
{
wrt = new Writer();
Thread trd = new Thread(new ThreadStart(delegate() {
lock (wrt)
{
Thread.Sleep(1000);
wrt.Write("1");
}
}));
trd.Start();
wrt.Write("0");
Console.ReadLine();
}
}
}
例外的输出是“10”,但输出是“01”。为什么?
【问题讨论】:
-
我希望它显示 01
-
lock将使用对象作为“隔离机制”来“隔离”代码块。您没有锁定对象..您正在隔离代码块。
标签: c# multithreading monitor thread-synchronization