As threads are executing concurrently and they are using shared resources, multiple threads may want to access the same resources simultaneously. This scenario may create thread interference and memory inconsistency. To prevent these errors, synchronization is used.
There are two ways to deal with this problem: synchronized methods and synchronized blocks.
Method Level Synchronization
In method-level synchronization, when one thread is executing the synchronized method, other threads will not get access to the method. At a time only one thread can execute a synchronized method.
Counter.java
public class Counter {
private int count;
public Counter() {
count = 0;
}
public Counter(int start) {
count = start;
}
// Synchronized method
public synchronized void increaseCount() {
System.out.print("Starting synchronized method increaseCount");
try {
Thread.sleep(5);
} catch (InterruptedException ie) {
}
count = count + 1;
System.out.print("Ending synchronized method increaseCount");
}
// Synchronized method
public synchronized int getCount() {
System.out.print("Synchronized method getCount");
return count;
}
}
CountingThreadSynchMethod.java
public class CountingThreadSynchMethod implements Runnable {
Counter myCounter;
int countAmount;
public CountingThreadSynchMethod(Counter counter, int amount) {
myCounter = counter;
countAmount = amount;
}
public void run() {
for (int i = 1; i <= countAmount; i++) {
myCounter.increaseCount();
}
}
public static void main(String args[]) throws Exception {
Counter c = new Counter();
Runnable runner = new CountingThreadSynchMethod(c, 10);
System.out.println("Starting counting threads");
Thread thread1 = new Thread(runner);
Thread thread2 = new Thread(runner);
Thread thread3 = new Thread(runner);
thread1.start();
thread2.start();
thread3.start();
thread1.join();
thread2.join();
thread3.join();
System.out.println("Counter value is " + c.getCount());
}
}
Block Level Synchronization
In block-level synchronization, when one thread is executing a synchronized block, other threads will not be able to enter the block. At a time only one thread can execute a synchronized block.
CountingThreadSynchBlock.java
public class CountingThreadSynchBlock implements Runnable {
StringBuffer buffer;
int counter;
public CountingThreadSynchBlock() {
buffer = new StringBuffer();
counter = 1;
}
public void run() {
synchronized (buffer) {
System.out.print("Starting synchronized block ");
int tempVariable = counter++;
String message = "Count value is : " + tempVariable + System.getProperty("line.separator");
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
buffer.append(message);
System.out.println("Ending synchronized block");
}
}
public static void main(String args[]) throws Exception {
CountingThreadSynchBlock block = new CountingThreadSynchBlock();
Thread thread1 = new Thread(block);
Thread thread2 = new Thread(block);
Thread thread3 = new Thread(block);
thread1.start();
thread2.start();
thread3.start();
thread1.join();
thread2.join();
thread3.join();
System.out.println(block.buffer);
}
}
Happy Exploring!
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.