简体   繁体   中英

Lock on object in multi threading

I have a question here in object locking in 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. 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. How this lock is shared by both thread-1 and thread-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.

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 .

Read this .

You should use a synchronized blocks using a specific lock object instead of synchronizes methods. 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.

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.

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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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