multithreading - Difference between class locking and object locking in Java -


People talk about two types of multi-threaded locking - objects and classes. In my knowledge, locking is done only on objects.

Case 1: We create objects on using new or factory methods etc.

  zero myMethod (type absolute) {// will lock on the example used to call this method}   

or <

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

or

  synchronize (obj1) {// specified obj1 will be locked on object}   

case 2: on java.lang.Class object

class Lock is called, and it can be used for stagnant areas or methods or blocks. Economic, because they went to the related class and shared between all objects and other class properties.

  Fixed zero synchronized method () {// Class object will lock}   

or

  static { Synchronize (SomeClass.class) {int a = 2; }}   
  • Why am I thinking of it as an object locking because sections have been loaded in the JVM's Method area, and all Static Properties are the java.lang.Class wrapped inside the object created by JVM. So behind the abstract, in its object locking and in the picture, we see class locking.
  • That's why I can also guess another thing. As soon as the object locked by a thread can not be taken from another thread, the class locking ( java.lang.Class example) is not the same as it is not released by the first thread. Works the way. / Li>
  • I want to know the case of synchronized static methods, lock in which category has been acquired by thread in two cases:

    1. This method It is called to the same

      This is my understanding yet. Add about or improve on topic.

      The only difference is that a static synchronize locks and examples on the class instance But a non-static synchronize method lock.

      People talk about two types of multi-threaded locking

      Object insent lock and lock style lock is a lock is confusingly both.

      item and class

      not true as you've already done

      Just because people say stuff it is not true. Often people are very nonsense, in fact, there are entire web sites not estimated in Java: p

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -