javamultithreadingconcurrencyrace-conditionmultiprocessor

Synchronization of data in multiple-core environment (Java-based)


this is my first question ever so please be gentle on me.

What happens when two threads, say t1 and t2, running on separate CPU cores invoke a synchronized method on a shared object AT THE SAME TIME, i.e. in the same nanosecond/processor cycle? How and by what element of the architecture is the lock resolved?

It may seem like an obvious question but i could not find anything on the internet. Thanks!


Solution

  • The spec simply says the synchronized method may only be entered by one thread at a time. What architecture mechanism is used would depend on the JVM.

    In the case of Oracle HotSpot (the JVM that almost everyone runs): the C++ code is GNU, and probably holds the answer for you.

    But if you just need the assurance that it "just works", the specification gives that to you.