In Java, a synchronized block is a way to synchronize access to a shared resource within a specific block of code. Unlike synchronized methods, which synchronize the entire method, synchronized blocks only synchronize the block of code within the curly braces {}. This can be useful if you need to synchronize access to a specific portion of a method, but not the entire method.
Here's an example that demonstrates the use of synchronized blocks in Java:
public class SynchronizedBlockExample {
private int count = 0;
private Object lock = new Object();
public void increment() {
synchronized (lock) {
count++;
}
}
public int getCount() {
return count;
}
}
In this example, we have a SynchronizedBlockExample class that has two methods, increment() and getCount(), that access a shared variable count. The increment() method contains a synchronized block that synchronizes access to the count variable using an object lock. Only one thread can enter the synchronized block at a time.
To use this class in a multithreaded environment, you can create multiple threads that access the increment() method to increment the value of count, and another thread that accesses the getCount() method to get the value of count. Here's an example:
public class SynchronizedBlockThreadExample {
public static void main(String[] args) throws InterruptedException {
SynchronizedBlockExample example = new SynchronizedBlockExample();
Runnable runnable = () -> {
for (int i = 0; i < 100000; i++) {
example.increment();
}
};
Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("Count: " + example.getCount());
}
}
In this example, we have a SynchronizedBlockThreadExample class that creates two threads that access the increment() method of SynchronizedBlockExample. Each thread increments the value of count 100,000 times. After both threads have finished their execution, the main thread accesses the getCount() method to get the final value of count.
It's important to note that synchronized blocks can still cause performance issues in multithreaded environments, as threads may need to wait for a lock to be released. Therefore, you should use synchronized blocks only when necessary and with caution.