Sync with internal locks

I read B. Goetz Java concurrency in pratice , and now I am in the section on locking. He said that

The fact that every object has a built-in lock is just a convenience, so you do not need to explicitly create lock objects.

9 In retrospect, this design decision was probably bad: not only can this be confusing, but it also forces JVM developers to compromise between object size and lock performance.

Since I am new to concurrency, it is not obvious what design decision he was talking about. Creating an explicit lock object is bad in terms of performance, isn't it?

+4
source share
1 answer

I never implemented the JVM myself or even participated in its development, but I can imagine a compromise.

The first idea that comes to mind to associate an internal lock with each object is to do this: to associate a lock with its state (I am blocked, by what thread) for each object created in the JVM, it is placed along with the rest of the state of the object . It must be effective. However, it also forces each created object to occupy additional memory space, and this space will in most cases never be used (since synchronization is not shared, and most objects are never used as a lock).

, - , , . , , , .. .

, , ( ), . , , , , , .

+2

Source: https://habr.com/ru/post/1621220/


All Articles