1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc)
Java online test
JSP online test
ORACLE online test
Hibernate online test
Servlet online test
Struts online test
EJB online test
C online test
C++ online test
Aptitude online test

Singleton Double-checked locking in Java?

public static Singleton getInstance()
{
if (instance == null)
{
synchronized(Singleton.class) { //1
if (instance == null) //2
instance = new Singleton(); //3
}
}
return instance;
}
Thread 1 enters the getInstance() method.


Thread 1 enters the synchronized block at //1 because instance is null.


Thread 1 is preempted by thread 2.


Thread 2 enters the getInstance() method.


Thread 2 attempts to acquire the lock at //1 because instance is still null. However, because thread 1 holds the lock, thread 2 blocks at //1.


Thread 2 is preempted by thread 1.


Thread 1 executes and because instance is still null at //2, creates a Singleton object and assigns its reference to instance.


Thread 1 exits the synchronized block and returns instance from the getInstance() method.


Thread 1 is preempted by thread 2.


Thread 2 acquires the lock at //1 and checks to see if instance is null.


Because instance is non-null, a second Singleton object is not created and the one created by thread 1 is returned.

Online Practice Test

Java online test
JSP online test
ORACLE online test
Hibernate online test
Servlet online test
Struts online test
EJB online test
C online test
C++ online test
Aptitude online test

Reviews and Comments


PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016


About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy