user1832435
user1832435

Reputation: 31

Two java threads at exactly same milli second

If two threads try to access the synchronized method at the VERY exact same time with exact milli-second or (whatever minimum CPU unit is).. What will happen? How JVM/System will resolve this issue if they have same priority as well. Will it create deadlock? Also, where this information about locks is stored?

Upvotes: 3

Views: 95

Answers (2)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340933

Entering synchronized block is translated internally to obtaining some atomic mutex or semaphore in the operating system. These constructs are guaranteed to be atomic. Thus nothing wrong will happen, one method is always first. Even in a multi-core, multi-CPU machine only one thread can obtain semaphore/mutex at once.

If will definitely not create a deadlock. To create a deadlock you need two resources to compete.

Upvotes: 2

asteri
asteri

Reputation: 11592

That's the whole point of threading. The operating system's job scheduler will allow one to go before the other. No deadlock will occur.

Actually, the job scheduler is for heavyweight forking of processes. The JVM handles which Threads have a turn to execute or enter a synchronized block.

Upvotes: 5

Related Questions