Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions notes/01-singleton-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class Database {

### Double-checked locking

The above code is thread-safe. However, it is not efficient. If two threads call the getInstance() method at the same time, both threads will check if the instance variable is null. Both threads will find that the instance variable is null. Both threads will wait for the lock to be released. Once the lock is released, one thread will create a new instance of the Database class. The other thread will wait for the lock to be released. Once the lock is released, it will create a new instance of the Database class. This will result in two instances of the Database class. To make the above code efficient, we can use double-checked locking.
The above code is thread-safe. However, it is not efficient. If a database instance has been created, and two threads try to access it, only one thread can call getInstance method, and the other must wait until the lock is released. To make the above code efficient, we can use double-checked locking.

```java
public class Database {
Expand Down Expand Up @@ -266,4 +266,4 @@ Database database = new Database.DatabaseBuilder()

## Reading list
* [Telescoping constructor anti-pattern](https://www.vojtechruzicka.com/avoid-telescoping-constructor-pattern/)
* [Why objects should be immutable?](https://octoperf.com/blog/2016/04/07/why-objects-must-be-immutable)
* [Why objects should be immutable?](https://octoperf.com/blog/2016/04/07/why-objects-must-be-immutable)