From 1c1cd9286defc866cbfafe6f8ce0ab92c5ba6e00 Mon Sep 17 00:00:00 2001 From: nitinvishwari <71441089+nitinvishwari@users.noreply.github.com> Date: Fri, 25 Jul 2025 15:32:38 -0700 Subject: [PATCH 1/2] Not a good idea to use synchronized everytime while calling getIntance. I am commiting the better alternative. --- solutions/java/src/carrentalsystem/CarRentalSystem.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/solutions/java/src/carrentalsystem/CarRentalSystem.java b/solutions/java/src/carrentalsystem/CarRentalSystem.java index 7736b606..e9307fcc 100644 --- a/solutions/java/src/carrentalsystem/CarRentalSystem.java +++ b/solutions/java/src/carrentalsystem/CarRentalSystem.java @@ -11,7 +11,7 @@ import java.util.concurrent.ConcurrentHashMap; public class CarRentalSystem { - private static CarRentalSystem instance; + private static CarRentalSystem instance = new CarRentalSystem(); private final Map cars; private final Map reservations; private final PaymentProcessor paymentProcessor; @@ -23,9 +23,6 @@ private CarRentalSystem() { } public static synchronized CarRentalSystem getInstance() { - if (instance == null) { - instance = new CarRentalSystem(); - } return instance; } From 583ed40db9c9e9900249ed7da6fbf7e3bd493f29 Mon Sep 17 00:00:00 2001 From: nitinvishwari <71441089+nitinvishwari@users.noreply.github.com> Date: Fri, 25 Jul 2025 15:46:14 -0700 Subject: [PATCH 2/2] removed synchronized --- solutions/java/src/carrentalsystem/CarRentalSystem.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/java/src/carrentalsystem/CarRentalSystem.java b/solutions/java/src/carrentalsystem/CarRentalSystem.java index e9307fcc..0e871627 100644 --- a/solutions/java/src/carrentalsystem/CarRentalSystem.java +++ b/solutions/java/src/carrentalsystem/CarRentalSystem.java @@ -22,7 +22,7 @@ private CarRentalSystem() { paymentProcessor = new CreditCardPaymentProcessor(); } - public static synchronized CarRentalSystem getInstance() { + public static CarRentalSystem getInstance() { return instance; }