I was asked about the singleton mode when I went to the interview today. I wrote it very proudly and thought it was safe. I found that I was wrong. I didn't take it into consideration at two points.
(1) What should I do if I want to ensure that the instance object cannot be new in other classes?
It is important to write a private constructor.
- private SingleMode() {
- }
(2) There can be only one instance in the entire application:
I wrote this before:
In this way, you can determine whether it is null. If it is null, a new instance is created. If it is not null, the previous one is returned.
- public static SingleMode mInstance() {
- if (single==null) {
- single=new SingleMode();
- }
- return single;
- }
(3) The problem arises again later. What if two threads call this method at the same time? Two new instances may be generated.
So what we need to do is ensure that only one thread can generate a new instance at the same time.
So I made the following improvements:
- public static SingleMode getInstance() {
- synchronized (SingleMode.this) {
- if (single==null) {
- single=new SingleMode();
- }
- }
- return single;
- }
(4) In this way, we can ensure that only one thread can generate an instance at the same time, but the problem arises again. I added a synchronization lock to this class,
That is to say, at the same time, only one thread can access this class,
Other threads will not be able to access other methods of this Singleton class. Is it feasible to change it to the following method?
- public synchronized static SingleMode mInstance() {
- if (single==null) {
- single=new SingleMode();
- }
- return single;
- }
(5) adding a synchronization lock to the method ensures that only one thread can call the method at the same time, but the problem persists,
At a certain time point, only one thread can obtain this instance. If there are multiple threads, wait. This will affect the program performance.
Then I can improve the synchronization method and make another judgment. In this case,
You don't have to worry about getting the instance where the thread is located every time. The Code is as follows:
- public static SingleMode getInstance() {
- if(single==null) {
- return mInstance();
- }
- return single;
- }
- public synchronized static SingleMode mInstance() {
- if (single==null) {
- single=new SingleMode();
- }
- return single;
- }
(6) Well, after continuous improvement and finally completion, I feel that I think the problem is still not perfect, so I will not think more clearly.
The Singleton mode of the full version is as follows. I didn't see this on the Internet, so I hope to share it with you,
Both the interview and subsequent code are helpful to everyone.
- Public class SingleMode {
- Private static SingleMode single = null;
-
- // (1) private constructor
- Private SingleMode (){
- }
- Public static SingleMode getInstance (){
- If (single = null ){
- Return mInstance ();
- }
- Return single;
- }
- Public synchronized static SingleMode mInstance (){
- If (single = null ){
- Single = new SingleMode ();
- }
- Return single;
- }
- }
Conclusion: Through this supplement to the singleton model, I feel that I have many things to add and share with you, hoping to help you.
PS: Pay attention to the details. Details determine success or failure.