fork download
  1. import lombok.Getter;
  2. import lombok.SneakyThrows;
  3.  
  4. @Getter // Lombok annotation to generate getter methods for the fields
  5. class Database {
  6. private static Database database;
  7. private final String connection;
  8.  
  9. private Database(String connection) {
  10. this.connection = connection;
  11. }
  12.  
  13. @SneakyThrows // Lombok annotation to handle checked exceptions
  14. public static synchronized Database getInstance(String connection) {
  15. System.out.printf("Thread %s entered a critical section\n", Thread.currentThread().getName());
  16.  
  17. if (database == null) {
  18. System.out.printf("Thread %s initializing the database instance\n", Thread.currentThread().getName());
  19.  
  20. Thread.sleep(1000); // Simulate resource-intensive initialization
  21. database = new Database(connection);
  22. }
  23.  
  24. System.out.printf("Thread %s returns...\n", Thread.currentThread().getName());
  25.  
  26. return database;
  27. }
  28. }
  29.  
  30. public class Main {
  31. public static void main(String[] args) throws InterruptedException {
  32. // Create two threads that attempt to access the singleton instance
  33.  
  34. new Thread(() -> {
  35. Database.getInstance("Dummy connection string of " + Thread.currentThread().getName());
  36. }).start();
  37.  
  38. new Thread(() -> {
  39. Database.getInstance("Dummy connection string of " + Thread.currentThread().getName());
  40. }).start();
  41.  
  42. // Allow some time for threads to complete their execution
  43. Thread.sleep(2000);
  44.  
  45. // Retrieve the connection from the singleton instance and log the result
  46. String finalConnection = Database.getInstance(null).getConnection();
  47. System.out.printf("Database connection: %s\n", finalConnection);
  48. }
  49. }
  50.  
  51. /*
  52.  
  53. Output without synchronized:
  54.  
  55. Thread Thread-1 entered a critical section
  56. Thread Thread-1 initializing the database instance
  57. Thread Thread-0 entered a critical section
  58. Thread Thread-0 initializing the database instance
  59. Thread main entered a critical section
  60. Database connection: dummy_connection of Thread-1
  61.  
  62. Output with synchronized:
  63. Thread Thread-1 entered a critical section
  64. Thread Thread-1 initializing the database instance
  65. Thread Thread-0 entered a critical section
  66. Thread main entered a critical section
  67. Database connection: Dummy connection string of Thread-1
  68. */
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: package lombok does not exist
import lombok.Getter;
             ^
Main.java:2: error: package lombok does not exist
import lombok.SneakyThrows;
             ^
Main.java:4: error: cannot find symbol
@Getter // Lombok annotation to generate getter methods for the fields
 ^
  symbol: class Getter
Main.java:13: error: cannot find symbol
    @SneakyThrows // Lombok annotation to handle checked exceptions
     ^
  symbol:   class SneakyThrows
  location: class Database
Main.java:46: error: cannot find symbol
        String finalConnection = Database.getInstance(null).getConnection();
                                                           ^
  symbol:   method getConnection()
  location: class Database
5 errors
stdout
Standard output is empty