|Home |Login |Registration |Struts Step by Step Tutorial |Hibernate Step by Step Tutorial |Spring Step by Step Tutorial |JSP Step by Step Tutorial |JDBC Step by Step Tutorial |Web Services Step by Step Tutorial |EJB fundamentals | ORACLE Step by Step Tutorial | SCJP 5.0 and SCJP 6.0 Study Guide | SCWCD 5.0 Study Guide | SCJP Tips
Java mock test | SCJP mock test | SCJP DUMP | SCBCD mock test |Java online test exam | 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

Java interview questions | JSP interview questions | ORACLE interview questions | Hibernate interview questions | Servlet interview questions | Struts interview questions | JDBC interview questions | C/C++ interview questions | Spring interview questions | JMS interview questions | Informatica interview questions | EJB interview questions | OOPS and Design Pattern interview questions
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) JobsJobs and Walkins

OOPs and Design Pattern interview questions

  • What is Observer pattern ? Explain with Code Example ? view answer
  • "is-a" relationship describes inheritance relationship between objects view answer
  • has-a relationship, which is also called object composition view answer
  • The four main concepts are involved in OOP view answer
  • Example Java Code For Association & Aggregation & Composition view answer
  • What are the common things need to consider while Creating and Destroying Objects view answer
  • what is the difference between encapsulation and data hiding? explain with Code example? view answer
  • Dependency In Java view answer
  • Is Singleton class is serialized ? view answer
  • What is Singleton ? and Double-checked locking ? view answer
  • What is Factory Method pattern? view answer
  • What is Adapter pattern ? view answer
  • What is Proxy and Decorator patterns ? With Java Example ? view answer
  • Brief Introduction to OOP Concepts view answer
  • What is Abstract Factory Pattern ? With Code Example ? view answer

!!! OOPs and Design Pattern interview questions !!!

What is Singleton ? and Double-checked locking ?




Need to follow :
* private constructor , can't create object from outside of the class
* Synchronized methods are used to ensure that the class is thread-safe.
* This class cannot be subclassed because the constructor is private. This may or may
not be a good thing depending on the resource being protected.



//Single-checked locking
//Thread 1 enters the synchronized block, and, before it can assign the singleton member variable, the thread is preempted. Subsequently, another thread can enter the if block. The second thread will wait for the first thread to finish, but we will still wind up with two distinct singleton instances


public static Sequence getInstance() {
if(singleton == null) {
synchronized(Sequence.class) {
singleton = new Sequence();
}
}
return singleton;
}


//final and best solution is

public class Sequence {
private static Sequence singleton;
private Sequence()
{
}

//Double-checked locking
//Imagine Thread 1 enters the synchronized block and is preempted.Subsequently, a second thread enters the if block.When Thread 1 exits the synchronized block, Thread 2 makes a second check to see if the singleton instance is still null.Since Thread 1 set the singleton member variable, Thread 2's second check will fail, and a second singleton will not be created.


public static Sequence getInstance() {
if(singleton == null) {
synchronized(Sequence.class) {
if(singleton == null) {
singleton = new Sequence();
}
}
}
return singleton;
}

}









Suggested Jobs

   More Jobs >>

The information you are posting should be related to java and ORACLE technology. Not political.