0

One of the first things we all learnt about concurrency in Java is that we use locks (synchronized keyword, Lock/ReadWriteLock interface) to protect against concurrent access. For example:

synchronized void eat(){
        //some code
    }

In theory, this method eat() can be executed by a single thread. Even though there are many threads waiting to execute it, only one will take the lock. But then comes parallelism, which made me think twice about what I just said.

I have 4 core CPU. That means I can do 4 tasks parallelly. Yes, lock can be taken by a single thread. But could it happen that 4 threads call method eat() and take a lock at the LITERALLY same time, although there is a lock that needs to be acquired to actually do anything?

Can something like that even happen in Java? I guess it can't but I had to ask this. And how is it even dealing with a case I just said?

Stefan
  • 969
  • 6
  • 9
  • [You can actually have way more threads running than physical cores.](https://stackoverflow.com/questions/3126154). And no, two threads can not obtain the [monitor](https://en.wikipedia.org/wiki/Monitor_(synchronization)) of the instance you're synchronizing on at once. Only one thread can do that - which is the magic of the keyword synchronized – Blake Oct 29 '20 at 04:21
  • Does this answer your question? [How Synchronization works in Java?](https://stackoverflow.com/questions/11184642/how-synchronization-works-in-java) – Blake Oct 29 '20 at 04:26
  • 2
    The _exact point_ of the `synchronized` keyword is to require the JVM to take specific measures to prevent this from happening. – chrylis -cautiouslyoptimistic- Oct 29 '20 at 04:29
  • 1
    Just to be sure that it has been understood: since `eat()` is an instance method, it can be executed by multiple threads when being invoked on different objects. Only threads attempting to invoke the method *on the same object* may block each other. – Holger Oct 30 '20 at 11:52

2 Answers2

1

Short answer - JVM keeps a list of threads that are trying to get the lock.

More details

https://wiki.openjdk.java.net/display/HotSpot/Synchronization

Also, worth mentioning that list (aka "inflated lock") is only created when really required (== when contention on a lock happens).

Alexander Pavlov
  • 2,264
  • 18
  • 25
1

...4 threads...take a lock at the LITERALLY same time...

Can't happen. Any "synchronized" operation (e.g., "take a lock") must operate on the system's main memory, and in any conventional computer system, there is only one memory bus. It is physically impossible for more than one CPU to access the main memory at the same time.

If two CPUs decide to access the memory at literally the same time, the hardware guarantees that one of them will "win" the race and go first, while the other one is forced to wait its turn.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • "while the other one is forced to wait its turn." but its turn will happen in a few nanoseconds. It is software level responsibility to resolve case when multiple threads are trying to get the lock. – Alexander Pavlov Oct 30 '20 at 14:01