Shushan has road diligence as a path, and learning is boundless and hard work. Articles are continuously updated. You can search for [Xiaoqi JAVA Interview] on WeChat to read it for the first time, and reply to [Information] and there are benefits I have prepared for you! Reply to [Project] There are some project source codes that I have prepared for you. Reply [Resume Template] There is a resume template that I have prepared for you.
@[TOC]
foreword
Today is the weekend. Most people should rest comfortably at home, but our electronic sewing machine factory is still in full swing. This is the characteristic of the electronic factory. You can go to work every day and make money every day. Envy it.
I went back to the dormitory early after work today, because I had an interview appointment. To be honest, my face is numb now. I keep introducing myself, and I almost vomit.
1. Interview
Interviewer: Can you talk about the singleton pattern in design patterns?
Me: The singleton pattern is the simplest design pattern with only one special class called the singleton class in its core structure. The singleton mode can ensure that there is only one instance of a class in the system and that the instance is easy to be accessed by the outside world, thereby facilitating the control of the number of instances and saving system resources.
Interviewer: Can you tell us where we usually use the singleton pattern?
Me: For example, in the task manager in our computer, we can only open one task manager. This is a manifestation of the singleton mode. A singleton is only one.
Interviewer: Can you write a simple implementation of the singleton pattern?
1. First write a singleton class
public class Singleton {
private static Singleton instance = null; //静态私有成员变量
//私有构造函数
private Singleton(){
}
//静态公有工厂方法,返回唯一实例
public static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
2. Then write a test class to test
public class Test {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
//判断两个对象是否相同
if(s1 == s2){
System.out.println("两个对象是相同实例");
}else{
System.out.println("两个对象是不同的实例");
}
}
}
Interviewer: What is a hungry singleton and what is a lazy singleton?
Me: Hungry-style singleton instantiates the singleton object when the class is loaded, and lazy-style singleton instantiates the object when the singleton class method is called.
Example: Hungry Man Mode
public class EagerSingleton {
private static final EagerSingleton instance = new EagerSingleton();
private EagerSingleton(){
}
public static EagerSingleton getInstance(){
return instance;
}
}
Interviewer: What is the double-checked locking mechanism of the lazy singleton pattern?
Me: In the lazy mode, the object is instantiated by calling the getInstance() method. If it is multi-threaded, it will cause problems, so we need to check the locking mechanism to judge.
public class LazySingleton {
private volatile static LazySingleton instance = null;
private LazySingleton(){}
public static LazySingleton getInstance(){
//第一重判断
if(instance == null){
//锁定代码块
synchronized (LazySingleton.class){
//第二重判断
if (instance == null){
instance = new LazySingleton();//创建单例实例
}
}
}
return instance;
}
}
Interviewer: The guy's answer is really good, what can I get into the job?
Me: the year of the monkey, the month of the horse
2. Summary
The relevant content here has not been sorted out, and the article will continue to be updated later, and it is recommended to collect it.
The commands involved in the article must be knocked several times like me. Only in the process of knocking can you find out whether you really master the commands.
If you think my article is not bad, please like it. In addition, you can search for [Xiaoqi JAVA Interview] on WeChat to read it for the first time, and reply to [Information] and there are more benefits I have prepared for you! Reply to [Project] There are some project source codes that I have prepared for you. Reply [Resume Template] There is a resume template that I have prepared for you.
This article participated in the Sifu technical essay , and you are welcome to join.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。