Synchronization is crucial for thread safety when multiple threads access a common resource.
Source Code
class Counter {
private int count = 0;
// Synchronized method to control access
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}