Home > Back-end >  Singleton pattern of double lock
Singleton pattern of double lock

Time:09-18

Public synchronized static Single newInstance () {
If (single==null) {
Synchronized (Single class) {
If (single==null) {
Single=new single ();
}
}
}
The return single;
}
So lock right

CodePudding user response:

You added a lock in the method, that it is destined to have only one thread can enter the method, that when you add lock in method still have what meaning?

CodePudding user response:

No, it doesn't make sense to you double checking

CodePudding user response:

Methods don't have to lock, or don't have to lock, so what you mean

CodePudding user response:

https://blog.csdn.net/qinhaotong/article/details/87884048

CodePudding user response:

You said the singleton is called DCL (double check the lock) double check, not double lock, lock, methods methods don't have the lock inside, should get rid of a synchronized method, statement for single variable volatile,
https://www.cnblogs.com/hexinwei1/p/9909555.html

CodePudding user response:

 

Public static Single newInstance () {
If (single==null) {
Synchronized (Single class) {
If (single==null) {
Single=new single ();
}
}
}
The return single;
}


even after this lock may be due to the compiler optimization problem, lead to an error,

About this, you can refer to my blog: https://blog.csdn.net/zy353003874/article/details/106727869

CodePudding user response:

Synchronized and methods of the static methods on internal synchronized code block, both choose a can,
On the main such two locks, no meaning, two kinds of usage, is to use. The class locked,
  • Related