Question 4 (c) What is Synchronization? Explain how methods are synchronized in Java, with the help of an example.
Answer:
Synchronization in Java:
Synchronization in Java is a mechanism used to control the access to shared resources or critical sections of code in a multi-threaded environment. It ensures that only one thread can access a synchronized block or method at a time, preventing concurrent access and potential data inconsistencies or race conditions
Methods Synchronization in Java:
In Java, synchronization can be achieved by using the `synchronized` keyword. There are two ways to synchronize methods:
1. Synchronizing an Entire Method:
- By declaring a method as `synchronized`, only one thread can execute that method at a time.
- The synchronization is achieved by locking the object (monitor) associated with the method.
- Other threads attempting to execute the same synchronized method will be blocked until the lock is released.
2. Synchronizing a Block of Code:
- Instead of synchronizing the entire method, we can use synchronized blocks to synchronize specific sections of code.
- A synchronized block is surrounded by the `synchronized` keyword followed by an object (monitor) used for synchronization.
- Only one thread can execute the synchronized block at a time, while other threads are blocked until the lock is released.
Example of Method Synchronization in Java:
class Counter {
private int count = 0;
public synchronized void increment() {
count++;
System.out.println("Incremented count: " + count);
}
public void performTask() {
// Non-synchronized code
System.out.println("Performing the task
synchronized (this) {
// Synchronized block
count--;
System.out.println("Decremented count: " + count);
}
}
}
public class SynchronizationExample {
public static void main(String[] args) {
Counter counter = new Counter();
// Create multiple threads to increment the count
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 5; i++) {
counter.increment();
}
});
Thread thread2 = new Thread(() -> {
for (int i = 0; i < 5; i++) {
counter.increment();
}
});
// Create a thread to perform the task
Thread thread3 = new Thread(counter::performTask);
// Start the threads
thread1.start();
thread2.start();
thread3.start();
}
}
In this example, we have a "Counter" class with two synchronized methods: "increment()" and "performTask()". The `increment()` method is synchronized by using the "synchronized" keyword in the method declaration. The "performTask()" method uses a synchronized block by surrounding the critical section of code with the "synchronized" keyword and "this" as the monitor.
In the "main()" method, we create multiple threads: "thread1" and "thread2" to increment the count and "thread3" to perform the task. Each thread calls the corresponding method on the "Counter" object.
When the program runs, the synchronized methods ensure that only one thread can access them at a time. The output will show the incremented and decremented counts in a synchronized manner
0 Comments
Please do not enter any spam link in the comment box.