What is class level, object level, explicit and internal locking?

I went through multi-threaded Java concepts. The more I worry about them, the more embarrassed.

Right now I don’t understand the difference between class level, object level, explicit and internal blocking in Java. Can someone please let me know what that is? Also, if I can understand some examples, it will be very useful for me.

+2
java multithreading locking
source share
4 answers

Explicit vs Inner

When you use synchronized for an object or indirectly as part of a method signature, you create an inline lock . You rely on a built-in lock associated with all objects and classes.

Explicit locking is provided in Java 5+ in the java.util.concurrent.locks package. The most commonly used class is probably ReentrantLock . They provide alternatives to using internal locks and offer features that are not possible with internal locks.

Class level and object level

This difference applies only to internal locks. If you have a synchronized static method , the inline lock used will be associated with the class object itself. If you synchronize an instance of an object (or have a synchronous instance method), it will be object-level locking.


additional literature

Brian Goetz Java Concurrency in Practice is a great book for understanding the nightmarishly confusing world of multi-threaded Java programming.

+2
source share

When you use the Synchronized keyword, it uses built-in locks or monitors. Every object in Java has a built-in lock. Whenever a thread tries to access a synchronized block or method , it receives a built-in lock or monitor on this object or an object level lock . In the case of static methods, the thread gets a lock on the class object .

 public synchronized void doAtomicTransfer(){ //enter synchronized block , acquire lock over this object. operation1() operation2(); } // exiting synchronized block, release lock over this object. 

The internal locking mechanism may have some functional limitations, such as:

  • Cannot interrupt a thread waiting for a lock (lock is interrupted).
  • You cannot try to acquire a lock without wanting to wait for it forever (try locking). Only one thread can hold the lock at once: there is no way, for example, to allow several threads to simultaneously block for read-only access.
  • It is not possible to implement locks without locks, because the built-in locks must be released in the same block in which they were acquired.

Explicit locks are useful when you need to overcome some of the disadvantages of built-in synchronization. In particular, they have the following functions:

  • The thread may try to block the lock;
  • A thread can give a timeout value for an attempt to capture a lock;
  • Read / write locks are supported, i.e. locks allowing multiple parallel readers if the lock is not locked for writing;
  • The traditional wait / notification metaphor expands to allow conditions (see below);
  • Equity support (if multiple threads are waiting for a lock, they acquire first-in-first-order when it becomes available);
  • Ability to block outside the block: for example, one method can transfer the blocking object to another thread;
  • You can block it to find out, for example, if they currently have any threads waiting to be received.
+1
source share

“class-level locking” and object-level locking are artificial ideas created by authors who probably don’t have a deep understanding of how Java built-in locking works.

class-level locking is as follows:

 class Foobar { static synchronized void moo() { ... } } 

But this construct is actually just a shorthand way of writing:

 class Foobar { static void moo() { synchronized (Foobar.class) { ... } } } 

And an object level lock that looks like this:

 class Foobar { synchronized void baa() { ... } } 

There is nothing short of abbreviation:

 class Foobar { static void baa() { synchronized (this) { ... } } } 

So, under the "class level lock" and "object level" there is only one concept: the synchronized block:

 synchronized(objectReference) {...} 

All you need to know is that the JVM does not allow you to synchronize more than one thread on the same object at the same time.

When the data you want to protect is global, then it makes sense to synchronize the global singleton object when accessing the data. Foobar.class - global singleton.

When the data you want to protect is completely contained in any instance of the object, then it makes sense to synchronize something related to this instance, or, in the instance itself (i.e. this ).

0
source share

Object level locking is a mechanism when you want to synchronize a non-static method or non-static block of code so that only one thread can execute a block of code for a given instance of the class. This should always be done to ensure the safe flow of instance-level data.

 public class DemoClass { public synchronized void demoMethod(){} } or public class DemoClass { public void demoMethod(){ synchronized (this) { //other thread safe code } } } or public class DemoClass { private final Object lock = new Object(); public void demoMethod(){ synchronized (lock) { //other thread safe code } } } 

Class-level locking prevents multiple threads from being included in a synchronized block in any of the available instances at run time. This means that if at run time there are 100 instances of DemoClass, then only one thread will be able to execute demoMethod () in any instance at a time, and all other instances will be blocked for other threads. This should always be done to ensure the security of static data.

 public class DemoClass { public synchronized static void demoMethod(){} } or public class DemoClass { public void demoMethod(){ synchronized (DemoClass.class) { //other thread safe code } } } or public class DemoClass { private final static Object lock = new Object(); public void demoMethod(){ synchronized (lock) { //other thread safe code } } } 
-one
source share

All Articles