java方法同步对象
问题描述:
我只是想确定我明白了以下的权利。java方法同步对象
- 方法上的synchronized关键字禁止在类的一个实例上同时运行两个此类方法。
- 同步对象是有问题的实例。
如果是这样的话下面的例子应该是正确的
class Example
{
public synchronized void method1()
{
// mark 1 - never here when other thread at mark 2 or 4
}
public synchronized void method2()
{
// mark 2 - never here when other thread at mark 1 or 4
}
public void method3()
{
// mark 3 - may be (!) here when other thread at mark 1, 2 or 4
synchronized (this)
{
// mark 4 - never here when other thread at mark 1 or 2
}
}
}
THX一个“是”或篡改。 b
答
你所说的话是正确的。
,并添加一两件事,如果该方法是一个静态方法,该类是锁。
感谢您的快速回复 – 2011-04-13 10:53:28
您的链接很有趣,虽然'不使用同步(这个)''在我看来,就像普通的经验法则一样,这不是普遍适用的...... – 2011-04-13 11:47:28