Waiting on access to mutex until another thread finishes and releases the lock


public class WaitingAccessMutexUntilAnotherThreadFinishesAndReleasesTheLock {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread() + " started");
        
        new Thread(() -> {
            System.out.println(Thread.currentThread() + " started");
            doSth1();
            // doSth2();
        }).start();

        doSth1();
    }
    
    public synchronized static void doSth1() {
        try {
            System.out.println(Thread.currentThread() + " doSth1 begin");
            Thread.sleep(5000);
            System.out.println(Thread.currentThread() + " doSth1 end");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized static void doSth2() {
        try {
            System.out.println(Thread.currentThread() + " doSth2 begin");
            Thread.sleep(5000);
            System.out.println(Thread.currentThread() + " doSth2 end");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
}

Output:
Thread[main,5,main] started
Thread[main,5,main] doSth1 begin
Thread[Thread-0,5,main] started
Thread[main,5,main] doSth1 end
Thread[Thread-0,5,main] doSth1 begin
Thread[Thread-0,5,main] doSth1 end

Explanation:
It does not matter if the Thread-0 wants to acccess doSth1 or doSth2 – the result is the same as they are both synchronized on the same lock. So Thread-0 needs to wait for main thread to finish sleeping while keeping the lock. When main thread releases the lock by exiting doSth1, then Thread-0 can execute synchronized code from doSth1 or doSth2.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s