if (searchBox == null) { //1
synchronized (SearchBox.class) {
if (searchBox == null) { //2
searchBox = new SearchBox();
}
}
}
}
here is my custom class for singleton pattern. in this code, I use double-checked locking as above. As I read many posts on some source, they say that double-check is useful because it prevents two concurrent threads run at same times make two different objects. As per threading concept, at a time an only single thread gets executed by thread scheduler. Then how 2 threads will try to execute the above code.
Please explain to me. What have I understood wrong?
Thanks :)
Aucun commentaire:
Enregistrer un commentaire