Home  >  Q&A  >  body text

java - 一个类的对象锁只有一个,类锁呢?

一个类的对象锁只有一个,如果有几个非静态函数都是synchronized,在某一时刻只有一个线程能调用其中一个函数

假如一个类有几个静态函数是synchronized,在某一时刻只有一个线程能调用其中一个静态函数吗?也就是类锁也只有一个吗?

高洛峰高洛峰2744 days ago700

reply all(6)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 10:51:00

    The previous one locks the instance object, locking the current object. If there are multiple instance objects, these synchronizedmethods are not synchronized. The second type of lock is a class object. There is only one class object, so it is synchronized.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:51:00

    public class Foo {
    
        synchronized void test1() {
            //to implements
        }
    
        void test2() {
            synchronized(this) {
                //to implements
            }
        }
    
        synchronized static void test3() {
            //to implements
        }
    
        static void test4() {
            synchronized(Foo.class) {
                //to implements
            }
        }
    }

    As shown in the above code, the test1 method is equivalent to test2. When this is the same object, blocking will occur. Of course, it doesn't matter if they are different objects, because this is different. It's called an object-level lock.
    test3 is equivalent to test4. Here, the class object is used as the lock, because generally there is only one class instance of a class, so it will be locked every time you enter this method. It's called a class-level lock.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:51:00

    • The lock used by the non-static synchronization method (A) is the current instance object itself. After A of one instance acquires the lock, other A’s of the instance must wait for the lock to be released. Multiple instances use different locks;

    • The lock used by the static synchronization method (B) is the class object itself. Once one B acquires the lock, other B must wait to release the lock, whether it is one instance or multiple instances;

    In addition, different locks are used between A and B, so there will be no competition;

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:51:00

    ClassWhen a class creates an object, it represents a common class. At this time, the "class lock" is the lock on this instance object

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:51:00

    You are talking about the concept of "mutex lock". There are two situations for the synchronized modification method:

    【Non-static method】

    When a method is modified by synchronized, the lock object is the object to which the current method belongs, which is this in the method.

    【Static method】

    When a static method is modified by synchronized, the object locked by the static method is the current class object (an instance of the Class class). Each class has a unique class object. How to obtain a class object: classname.class.

    For mutually exclusive scenarios, two points need to be understood:

    1. Static methods and non-static methods are declared synchronized at the same time, and there is a non-mutually exclusive relationship between them. The reason is that static methods lock the class object rather than the object to which the current method belongs.

    2. When Synchronized modifies two different pieces of code, but the lock objects are the same, the two threads are mutually exclusive when they call the two pieces of code respectively

    So when you say "only one thread can call one of the functions at a time" (i.e. mutual exclusion), the judgment condition is whether the lock objects are the same, regardless of the method type.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:51:00

    For synchronized modification of object methods, the lock is the object itself, which is this;
    For synchronized modification of static methods, the lock is the Class object itself, which is the class object created by the class loader;

    reply
    0
  • Cancelreply