以下两种lock的方法,有什么优缺点呢?小弟是新手,向各位大侠请教了。谢谢啦!public class Demo
{
public void DoThis()
{
lock (this)
{
// only one thread at a time can access the DoThis and DoThat methods
}
}
public void DoThat()
{
lock (this)
{
}
}
}public class Demo
{
private object syncRoot = new object();
public void DoThis()
{
lock (syncRoot)
{
// only one thread at a time can access the DoThis and DoThat methods
}
}
public void DoThat()
{
lock (syncRoot)
{
}
}
}
{
public void DoThis()
{
lock (this)
{
// only one thread at a time can access the DoThis and DoThat methods
}
}
public void DoThat()
{
lock (this)
{
}
}
}public class Demo
{
private object syncRoot = new object();
public void DoThis()
{
lock (syncRoot)
{
// only one thread at a time can access the DoThis and DoThat methods
}
}
public void DoThat()
{
lock (syncRoot)
{
}
}
}
private static object obj = new object();
使用的时候:
lock(obj)
lock(al)
{
}