WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Core Java · Intermediate · question 33 of 100

What is synchronization in Java? Why is it important?

📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

In Java, synchronization refers to the coordination of multiple threads to ensure that they do not interfere with each other when accessing shared resources or critical sections of code. When multiple threads access shared resources concurrently, there is a risk of race conditions, data inconsistencies, and other errors that can lead to program crashes or incorrect behavior.

Synchronization in Java is typically achieved through the use of the synchronized keyword, which can be applied to methods or code blocks. When a method or code block is marked as synchronized, Java ensures that only one thread can execute it at a time, preventing concurrent access to shared resources and ensuring that the code is executed atomically.

Here’s an example of how to use synchronization in Java:

class Counter {
    private int count;
    
    public synchronized void increment() {
        count++;
    }
    
    public int getCount() {
        return count;
    }
}

// create a new Counter object
Counter counter = new Counter();

// create two threads that increment the counter
Thread thread1 = new Thread(() -> {
    for (int i = 0; i < 1000000; i++) {
        counter.increment();
    }
});

Thread thread2 = new Thread(() -> {
    for (int i = 0; i < 1000000; i++) {
        counter.increment();
    }
});

// start the threads
thread1.start();
thread2.start();

// wait for the threads to finish
thread1.join();
thread2.join();

// print the final count
System.out.println("Count: " + counter.getCount());

In this example, we create a Counter class with a synchronized increment() method and a getCount() method. We then create two threads that increment the counter, and we use the join() method to wait for the threads to finish before printing the final count. Because the increment() method is synchronized, only one thread can execute it at a time, preventing concurrent access to the count variable.

Synchronization in Java is important because it ensures that multiple threads can safely access shared resources without interfering with each other. Without synchronization, concurrent access to shared resources can lead to race conditions, data inconsistencies, and other errors that can cause program crashes or incorrect behavior. By using synchronization, Java programmers can write multithreaded programs that are safe, reliable, and efficient.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Core Java interview — then scores it.
📞 Practice Core Java — free 15 min
📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

All 100 Core Java questions · All topics