Java Synchronization: Object-Level and Class-Level
Here's an implementation demonstrating both object-level and class-level synchronization in Java:
Object-Level Synchronization
Object-level synchronization locks on a specific instance of a class.
java
class Counter {
private int count = 0;
// Object-level synchronized method
public synchronized void increment() {
count++;
System.out.println(Thread.currentThread().getName() + " incremented count to: " + count);
}
public int getCount() {
return count;
}
}
public class ObjectLevelSyncDemo {
public static void main(String[] args) {
Counter counter = new Counter();
// Create multiple threads that share the same counter
Runnable task = () -> {
for (int i = 0; i < 5; i++) {
counter.increment();
try {
Thread.sleep(100); // Simulate some work
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
Thread thread1 = new Thread(task, "Thread-1");
Thread thread2 = new Thread(task, "Thread-2");
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Final count: " + counter.getCount());
}
}
Class-Level Synchronization
Class-level synchronization locks on the class itself (static synchronization).
java
class StaticCounter {
private static int count = 0;
// Class-level synchronized method
public static synchronized void increment() {
count++;
System.out.println(Thread.currentThread().getName() + " incremented static count to: " + count);
}
public static int getCount() {
return count;
}
}
public class ClassLevelSyncDemo {
public static void main(String[] args) {
// Create multiple threads that will share the static counter
Runnable task = () -> {
for (int i = 0; i < 5; i++) {
StaticCounter.increment();
try {
Thread.sleep(100); // Simulate some work
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
Thread thread1 = new Thread(task, "Thread-A");
Thread thread2 = new Thread(task, "Thread-B");
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Final static count: " + StaticCounter.getCount());
}
}
Combined Example
Here's a combined example showing both types of synchronization in one class:
java
class CombinedCounter {
private int instanceCount = 0;
private static int staticCount = 0;
// Object-level synchronized method
public synchronized void incrementInstance() {
instanceCount++;
System.out.println(Thread.currentThread().getName() +
" incremented instance count to: " + instanceCount);
}
// Class-level synchronized method
public static synchronized void incrementStatic() {
staticCount++;
System.out.println(Thread.currentThread().getName() +
" incremented static count to: " + staticCount);
}
public int getInstanceCount() {
return instanceCount;
}
public static int getStaticCount() {
return staticCount;
}
}
public class CombinedSyncDemo {
public static void main(String[] args) {
CombinedCounter counter1 = new CombinedCounter();
CombinedCounter counter2 = new CombinedCounter();
// Threads for instance method
Runnable instanceTask = () -> {
for (int i = 0; i < 3; i++) {
counter1.incrementInstance();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
// Threads for static method
Runnable staticTask = () -> {
for (int i = 0; i < 3; i++) {
CombinedCounter.incrementStatic();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
Thread t1 = new Thread(instanceTask, "InstanceThread-1");
Thread t2 = new Thread(instanceTask, "InstanceThread-2");
Thread t3 = new Thread(staticTask, "StaticThread-1");
Thread t4 = new Thread(staticTask, "StaticThread-2");
t1.start();
t2.start();
t3.start();
t4.start();
try {
t1.join();
t2.join();
t3.join();
t4.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Final instance count (counter1): " + counter1.getInstanceCount());
System.out.println("Final instance count (counter2): " + counter2.getInstanceCount());
System.out.println("Final static count: " + CombinedCounter.getStaticCount());
}
}
Key Differences
-
Object-Level Synchronization:
- Locks on the instance of the object
- Only one thread can execute synchronized instance methods of the same object
- Different threads can execute synchronized methods on different objects simultaneously
-
Class-Level Synchronization:
- Locks on the Class object of the class
- Only one thread can execute synchronized static methods of the class at a time
- Affects all instances of the class across the JVM
Both approaches ensure thread safety but at different levels of granularity.