Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews Java Programming Language Interviews:Advanced JavaCore JavaEclipseFull Stack Developer (Java)HibernateIBM WebSphereInternationalization LocalizationJ2EEJ2MEJ2SEJavaJava ANTJava AppletJava BeansJava ClassesJava EJB ProgrammingJava Game DeveloperJava GUI FrameworkJava JNDIJava JNIJava JSP ProgrammingJava Message Service (JMS)Java Multi-ThreadingJava Networking - Sockets and RMIJava PatternsJava SecurityJava Server FacesJava Servlet ProgrammingJava Software EngineerJava Swing ProgrammingJava TechnicalJava ThreadsJava Transaction APIJava Web ServicesJavaMailJBossJDBCJMSJSFPortal and PortletRMISpring FrameworkSr.Java Web DeveloperStrutsSunOneSwing AWTSWT JFace
Copyright © 2018. All Rights Reserved
Java Patterns Interview Question:
How do you write a Thread Safe Singleton?
Submitted by: AdministratorThe following are the steps to write a thread-safe singleton:
► Declare a Boolean variable, ‘instantiated', to know the status of instantiation of a class, an object of Object class and a variable of the same class with initial value as null.
► Write a private constructor of this class
► Override getInstance() method. Check the Boolean state of the instance variable ‘instantiated'. If it is false, write a ‘synchronized' block and test for the current class instance variable. If it is null create an object of the current class and assign it to the instance variable.
The following code illustrates this :
public class SampleSingleton {
private static boolean initialized = false;
private static Object obj = new Object();
private static SampleSingleton instance = null;
private SampleSingleton() {
// construct the singleton instance
}
public static SampleSingleton getInstance() {
if (!initialized) {
synchronized(obj) {
if (instance == null) {
instance = new SampleSingleton();
initialized = true;
}
}
}
return instance;
}
}
Submitted by: Administrator
► Declare a Boolean variable, ‘instantiated', to know the status of instantiation of a class, an object of Object class and a variable of the same class with initial value as null.
► Write a private constructor of this class
► Override getInstance() method. Check the Boolean state of the instance variable ‘instantiated'. If it is false, write a ‘synchronized' block and test for the current class instance variable. If it is null create an object of the current class and assign it to the instance variable.
The following code illustrates this :
public class SampleSingleton {
private static boolean initialized = false;
private static Object obj = new Object();
private static SampleSingleton instance = null;
private SampleSingleton() {
// construct the singleton instance
}
public static SampleSingleton getInstance() {
if (!initialized) {
synchronized(obj) {
if (instance == null) {
instance = new SampleSingleton();
initialized = true;
}
}
}
return instance;
}
}
Submitted by: Administrator
Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.