对象同步 [英] object synchronization
问题描述
当同步块用于对象实例上的同步时,线程竞争进入对象的隐式监视器。一旦任何特定线程进入监视器,任何其他线程都必须等待进入监视器。那么
When a synchronized block is used for synchronization on an object instance, then threads compete to get into object's implicit monitor. And once, any particular thread enters into the monitor, any other thread has to wait for entering into it. Then
synchronized(object){
// some code here
// no function call
}
不得强制执行任何特定类型的对象。因此,这里可以使用任何对象类型,因为每个对象都有其隐式监视器。
must not enforce taking any particular type of object. Thus any object type can be used here because every object has its implicit monitor.
请回复我是真的吗?
推荐答案
是的,每个Java对象都可以充当监视器。
Yes, every Java Object can act as a monitor.
因为这是一个如此简短的答案,奖金,这是一个有趣的读物: JVM是否为每个对象创建一个互斥锁以实现'synchronized'关键字?如果没有,怎么做?
And since this is such a short answer, for bonus, this is an interesting read: Does the JVM create a mutex for every object in order to implement the 'synchronized' keyword? If not, how?
另请注意,C#与其对象类似,但也有值类型(不是监视器)
Also note that C# does something similar with their objects, but also have value types (which are not Monitors)
这篇关于对象同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!