The difference between class locking and object locking in Java

People talk about two types of multithreaded locking - an object and a class. As far as I know, locking is performed only on objects.

Case 1: the objects we create using the new or factory methods, etc.

 void synchronized myMethod(Type param) { //will lock on the instance used to call this method } 

or

 synchronized(this) { //will lock on current object } 

or

 synchronized(obj1) { //will lock on specified obj1 object } 

Case 2: java.lang.Class objects

This is called class locking and can be used with static fields or methods or blocks because they belong to the class and are shared between all objects and other properties of the class.

 static void synchronized method() { //will lock the Class object } 

or

 static { synchronized(SomeClass.class){ int a = 2; } } 
  • Why I think of it as blocking objects, because classes are loaded into the method area in the JVM, and all the static properties of the class are wrapped inside the java.lang.Class object created by the JVM . Thus, behind the abstraction, its object lock and image, we see the class lock.
  • So, I can do one more thing. Just as objects locked by a thread cannot be acquired by another thread unless it is released by the first thread, class locking (an instance of java.lang.Class ) also works the same.
  • In the case of synchronized static methods, I want to know the lock by which the class receives the stream in the following two cases:

    • This method is called from the same class where it is defined.
    • This method is called from a derived class with the name of the derived class.

This is my understanding so far regarding the subject. Please add or correct.

+8
java multithreading locking thread-synchronization
source share
2 answers

The only difference is that static synchronized blocks the instance of the class and the non-static synchronized method blocks the instance.

People talk about two types of multithreaded blocking.

There are object instance locks and Lock style Lock . A Lock , vaguely, has both.

object and class

Not like you already worked out.

Just because people say things don't make it true. Often people talk a lot of bullshit. Actually there are whole websites dedicated to the non-meaning of Java .: P

+4
source share

People talk about two types of multithreaded locking - an object and a class.

A class is an object. In Java, there is only one type of lock: each object (including each class) has a mutex that can be locked by a synchronized or synchronized block. The object to be locked is implicit in the synchronized method: it is an instance of "this" for the instance method, and a class object for the static method.

One of the most common beginner mistakes is to think that two different threads cannot get into the same synchronized block at the same time. They can, and there are many questions and answers here on StackOverflow that prove this. Another mistake is to think that if one thread is synchronized on an object, then other threads will not be able to modify the object. They can and they do it.

Synchronization prevents the simultaneous synchronization of two or more threads on the same object. Which object is the correct object? All about protecting your data. If the structure you want to protect is a linked list, for example, then a good choice would be for any method that accesses the list for synchronization in the list header. If you want to protect global data (for example, static variables), you want to synchronize the global object (for example, the class object to which the variables belong). The important thing is that if you have read / write data (aka, "mutable data") that is accessed by more than one thread, then every method that accesses the same data must synchronize with the same lock.


In Java, there is another kind of lock, but not in Java; this is in the standard java library. It is accessible through objects that implement the java.util.concurrent.locks.Lock interface. Of course, the Lock object (like any object) also implements the first type of lock, but you should never synchronize on the Lock object unless you want people to show that you are an ignorant newbie.

Locking a java.util.concurrent style is more efficient than using synchronized blocks due to its explicit lock () and unlock () methods. For example, one way to lock a lock and another way to unlock it. This can lead to code that is hard to understand, and I would not do it if I didn’t have a very good reason, but sometimes there are good reasons.

+7
source share

All Articles