Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What will happen if the locks themselves get contended upon?

All objects in Java have intrinsic locks and these locks are used for synchronization. This concept prevents objects from being manipulated by different threads at the same time, or helps control execution of specific blocks of code.

What will happen if the locks themselves get contended upon - i.e. 2 threads asking for the lock at the exact microsecond.

Who gets it, and how does it get resolved?

like image 603
Question Everything Avatar asked Dec 30 '13 04:12

Question Everything


People also ask

What does locking refer to?

Locking is the way that SQL Server manages transaction concurrency. Essentially, locks are in-memory structures which have owners, types, and the hash of the resource that it should protect. A lock as an in-memory structure is 96 bytes in size.

What is lock de escalation and under what conditions is it required?

Lock de-escalation occurs when a system shifts lock from a whole page containing many items to only those items which it needs to access. So, locks are shifted to coarser granularity.

How do you unlock a table?

Use the UNLOCK TABLE statement in a database that does not support transaction logging to unlock a table that you previously locked with the LOCK TABLE statement. The UNLOCK TABLE statement is an extension to the ANSI/ISO standard for SQL.

Which of the following issues can be caused by unnecessary locking of database objects?

Well, there are a number of problems that can be caused by database locking. They can generally be broken down into 4 categories: Lock Contention, Long Term Blocking, Database Deadlocks, and System Deadlocks.


1 Answers

What will happen if the locks themselves get contended upon - i.e. 2 threads asking for the lock at the exact microsecond.

One thread will get the lock, and the other will be blocked until the first thread releases it.

(Aside: some of the other answers assert that there is no such thing as "at the same time" in Java. They are wrong!! There is such a thing! If the JVM is using two or more cores of a multi-core system, then two threads on different cores could request the same Object lock in exactly the same hardware clock cycle. Clearly, only one will get it, but that is a different issue.)

Who gets it, and how does it get resolved?

It is not specified which thread will get the lock.

It is (typically) resolved by the OS'es thread scheduler ... using whatever mechanisms that uses. This aspect of the JVM's behaviour is (obviously) platform specific.

If you really, really want to figure out precisely what is going on, the source code for OpenJDK and Linux are freely available. But to be frank, you don't need to know.

like image 193
Stephen C Avatar answered Sep 29 '22 13:09

Stephen C