0% found this document useful (0 votes)
9 views

Singleton in Java Is A Class With Just One Instance in Whole Java Application How To Create Thread-Safe Singleton in Java

Java

Uploaded by

Deepa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Singleton in Java Is A Class With Just One Instance in Whole Java Application How To Create Thread-Safe Singleton in Java

Java

Uploaded by

Deepa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

8.

What is difference between Executor.submit() and Executer.execute() method ?


There is a difference when looking at exception handling. If your tasks throws an exception and if it was submitted
with execute this exception will go to the uncaught exception handler (when you don't have provided one explicitly, the
default one will just print the stack trace to System.err). If you submitted the task with submit any thrown exception, checked
exception or not, is then part of the task's return status. For a task that was submitted with submit and that terminates with
an exception, the Future.get will re-throw this exception, wrapped in an ExecutionException.

9. What is the difference between factory and abstract factory pattern?


Abstract Factory provides one more level of abstraction. Consider different factories each extended from an Abstract
Factory and responsible for creation of different hierarchies of objects based on the type of factory.
E.g. AbstractFactory extended by AutomobileFactory, UserFactory, RoleFactory etc. Each individual factory would be
responsible for creation of objects in that genre.

You can also refer What is Factory method design pattern in Java to know more details.

10. What is Singleton? is it better to make whole method synchronized or only critical section
synchronized ?
Singleton in Java is a class with just one instance in whole Java application, for
example java.lang.Runtime is a Singleton class. Creating Singleton was tricky prior Java 4 but once Java 5
introduced Enum its very easy. see my article How to create thread-safe Singleton in Java for more details
on writing Singleton using enum and double checked locking which is purpose of this Java interview
question.

You might also like