public class DecideDancePair { DecideDancePair ddp1; StringBuffer ddp2 = new StringBuffer(); public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { synchronized (DecideDancePair.class) { System.out.println("Its weekend .... Hey Guys !! this is Nancy , who will dance with me? "); } } }).start(); new Thread(new Runnable() { @Override public void run() { synchronized (new DecideDancePair().ddp1) { System.out.println("Hey Nancy !! this is Peter , I will dance with you."); } } }).start(); new Thread(new Runnable() { @Override public void run() { synchronized (new DecideDancePair().ddp2) { System.out.println("Hey Nancy !! this is Rozer , I will dance with you."); } } }).start(); } }
==============================
Answer :
My new post is going to take little more time so thought of to post solution of this problem.
Nancy will always go for dance because she having Lock over a object which is not NULL. same with Rozer , he is also having a non-NULL lock. while poor Peter is caught with NULL object Lock so that piece of code will throw exception below and Hence Nancy and Rozer will de in dance 🙂 !!!
Code execution will looks like :
Its weekend .... Hey Guys !! this is Nancy , who will dance with me? Exception in thread "Thread-1" Hey Nancy !! this is Rozer , I will dance with you. java.lang.NullPointerException at BasicConcepts.DecideDancePair$2.run(DecideDancePair.java:26) at java.lang.Thread.run(Thread.java:745)
You post very interesting articles here. Your website deserves much more traffic.
It can go viral if you give it initial boost, i know very useful tool that can help you, simply search in google: svetsern traffic tips
LikeLike
This page really has all of the info I needed about this subject
and didn’t know who to ask.
LikeLike
lol , I got who danced with Nancy. Really very interesting question !!
LikeLike