java - Singleton with Enum called by threads -
hi can 1 me understand output of below snippet?
enum -
public enum singletonenum { instance; int num; public void demo(){ system.out.println(num); } }
main class -
public static void main(string[] args) { thread t1 = new thread(new runnable(){ public void run(){ singletonenum samp = singletonenum.instance; samp.num= 5; samp.demo(); } }); thread t2 = new thread(new runnable(){ public void run(){ singletonenum temp = singletonenum.instance; temp.num= 8; temp.demo(); } }); t1.start(); t2.start(); }
the o/p 8 , 8 .however if add private constructor in enum o/p 5 , 8. logic behind o/p being 8 , 8.
you code not synchronized. unspecified behavior, can observe in such case.
Comments
Post a Comment