简体   繁体   English

在多线程中锁定对象

[英]Lock on object in multi threading

I have a question here in object locking in java. 我在Java 对象锁定中有一个问题。 Ex. 例如 code: 码:

public class A 
{
    private static A a = null; // singleton instance

    private A()
    {

    }

    public static synchronized A getInst()
    {
        if (a == null)
        {
            a = new A();
        }
        return a;
    }

    public synchronized void method1()
    {
        //some action
    }

    public synchronized void method2()
    {
        //some action
    }
}

When a thread (say thread-1 ) is working inside method1() then thread-1 acquires lock on the singleton object. 当一个线程(例如thread-1 )在method1()内部工作时, 线程1获得单例对象的 But another thread (say thread-2 ) wants to enter in to the method2() then it will enter without waiting thread-1 to release the lock. 但是另一个线程(比如说thread-2 )想要进入method2()那么它将进入而无需等待 线程1释放锁。 How this lock is shared by both thread-1 and thread-2 ? 线程1线程2如何共享此锁?

Thanks 谢谢

But another thread (say thread-2) wants to enter in to the method2 then it will enter without waiting thread-1 to release the lock. 但是另一个线程(例如线程2)想要进入方法2,那么它将进入而无需等待线程1释放锁。

It won't. 不会的

At one time only one thread can acquire the lock on an object. 一次只有一个线程可以获取对象上的锁。

So, unless thread-1 releases the lock, thread-2 can't execute method2 . 因此,除非thread-1释放锁,否则thread-1 thread-2无法执行method2

Read this . 阅读

You should use a synchronized blocks using a specific lock object instead of synchronizes methods. 您应该使用使用特定锁定对象的synchronized块,而不是同步方法。 In such a case it is also easy to block just specific parts of methods where the lock is needed which might speed up multithreaded access if you don't need to synchronize the whole method. 在这种情况下,仅锁定需要锁定的方法的特定部分也很容易,如果您不需要同步整个方法,则可以加快多线程访问。

eg 例如

Object lock = new Object;
public void method1() {
    synchronized(lock) {
      // do stuff here
    }
}

In a singleton class only one instance will be available. 在单例类中,只有一个实例可用。 so when calling synchronized methods it will lock that single instance. 因此,在调用同步方法时,它将锁定该单个实例。 If more than one synchronized methods present,only one method will be executed at a time. 如果存在多个同步方法,则一次只能执行一个方法。 Other need to wait for previous method to release the lock. 其他需要等待以前的方法来释放锁。 So basically only one thread exists for this singleton class. 因此,对于该单例类基本上只存在一个线程。

If you want other methods not to be affected because of other methods lock, use synchronized block with object lock in singleton class. 如果您希望其他方法不会因为其他方法锁定而受到影响,请在单例类中使用带有对象锁定的同步块。

private final Object object1 = new Object();
private final Object object2 = new Object();

public void method1 {
    synchronized(object1) {
    ....
    }
}

public void method2 {
    synchronized(object2) {
    ....
    }
}

Both synchronized methods are instance methods so, multiple threads will not be able to access them at same time if all of them refer to same instance . 这两个同步方法都是实例方法,因此,如果所有线程都引用同一实例 ,则多个线程将无法同时访问它们。 In you case this is true, because you only have one instance of your class ( singleton pattern), and as a result if one thread is accessing method1() then other thread accessing method2() will go in wait state, until thread1 releases the lock or method1() is executed completely. 在您的情况下,这是正确的,因为您只有一个类的实例( 单例模式),因此,如果一个线程正在访问method1()则其他线程访问method2()将会进入等待状态,直到线程1释放该线程。锁或method1()已完全执行。

But another thread (say thread-2) wants to enter in to the method2 then it will enter without waiting thread-1 to release the lock. 但是另一个线程(例如线程2)想要进入方法2,那么它将进入而无需等待线程1释放锁。

While thread1 executes method1() which is synchronized using the instance of A, thread2 will wait before it can execute method2() which is also synchronized using the same instance of A (since it is Singleton ) 当线程1执行使用A 实例同步的method1() ,线程2将等待它执行也使用相同A 实例同步的method2() (因为它是Singleton

You can consider it as synchronized code blocks ( Critical Section ) instead of synchronized methods such as : 您可以将其视为同步代码块关键部分 ),而不是诸如以下的同步方法:

public void method1()
{
    synchronized (this)
    {
        //some action
    }
}

public void method2()
{
    synchronized (this)
    {
        //some action
    }
}

Considering such, you can see this refers to the same instance (object lock) for both methods 考虑到这一点,您可以看到this两个方法都引用相同的实例(对象锁)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM