object locking in java with realtime bathroom example


Assuming you know how to declare synchronized method. Going to discuss how object level locking works.

When there are two synchronized methods in one class, is it possible to execute them simultaneously by two different threads? The answer is, No. It is not possible .Because locking occurs at object level. The one who got the first chance have to release the lock to another one. He can execute the two methods simultaneously but until he releases the lock no one can execute these methods.

I will explain the same scenario in real time example.
For example there is a common bathroom to bath in the home.

 

The one who go to the room lock the door from inside and he can use shower or tub to bath. You can think shower and tub as two synchronized methods and the person as thread. The one who is waiting outside is another thread waiting for first one to come out.

How the inside person can use shower or tub, active thread can execute any synchronized method of the that object. After finishing his bath, he will open the door and come out ,then only next person got the chance to go in. Opening the door is like releasing the lock.

You can assume outside mirror as normal method (not synchronized) and it can be used by anyone. No need of lock in this case.

 

package com.myinterviewexperience; public class BathRoom { public synchronized void shower() { for (int i = 0; i < 10; i++) { try { Thread.sleep(200); System.out.println(Thread.currentThread().getName() + " using shower"); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void tub() { for (int i = 0; i < 10; i++) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out .println(Thread.currentThread().getName() + " using tub "); } } public void outSideMirror() { for (int i = 0; i < 10; i++) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " using mirror"); } } // this can be acceessed without lock public static void main(String[] args) { final BathRoom bathRoom = new BathRoom(); Thread t0 = new Thread(new Runnable() { public void run() { bathRoom.shower(); } }); t0.start(); Thread t1 = new Thread(new Runnable() { public void run() { bathRoom.tub(); } }); t1.start(); Thread t2 = new Thread(new Runnable() { public void run() { bathRoom.outSideMirror(); } }); t2.start(); Thread t3 = new Thread(new Runnable() { public void run() { bathRoom.outSideMirror(); } }); t3.start(); } }   Output: Thread-0 using shower Thread-3 using mirror Thread-2 using mirror Thread-0 using shower Thread-3 using mirror Thread-2 using mirror Thread-0 using shower Thread-3 using mirror Thread-2 using mirror Thread-0 using shower Thread-3 using mirror Thread-2 using mirror Thread-0 using shower Thread-2 using mirror Thread-3 using mirror Thread-0 using shower Thread-3 using mirror Thread-2 using mirror Thread-0 using shower Thread-2 using mirror Thread-3 using mirror Thread-0 using shower Thread-2 using mirror Thread-3 using mirror Thread-0 using shower Thread-2 using mirror Thread-3 using mirror Thread-0 using shower Thread-2 using mirror Thread-3 using mirror Thread-1 using tub Thread-1 using tub Thread-1 using tub Thread-1 using tub Thread-1 using tub Thread-1 using tub Thread-1 using tub Thread-1 using tub Thread-1 using tub Thread-1 using tub

Explanation:

If you observe until Thread-0 stop using shower,Thread-1 cannot use tub. But Thread-2 and Thread-3 are using mirror at the same time. This is because outSideMirror() method is not synchronized and it can be accessed without lock at any time.

Drop your questions or thoughts as comments below.
www.000webhost.com