Saturday, October 03, 2009

Why synchronization in java?

Consider the following programs

package org.vijayan.sample;

public class Main {

public static void main(String[] args) {
SampleFile file=new SampleFile("test.txt");
FileWriterThread mt1=new FileWriterThread(10000,"MT1", file);
FileWriterThread mt2=new FileWriterThread(20,"MT2", file);
mt1.start();
mt2.start();
}

}

package org.vijayan.sample;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


public class SampleFile {

private File file;
public SampleFile(String fileName){
file=new File(fileName);
file.delete();
}
public void writeMessage(int count, String message){
try {
FileWriter writer=new FileWriter(file,true);
for(int i=0;i
writer.write(message+"-"+i);
writer.write("\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

package org.vijayan.sample;

public class FileWriterThread extends Thread {

private SampleFile file;
private int count;
public FileWriterThread(int count, String name, SampleFile math){
super(name);
this.file=math;
this.count=count;
}
@Override
public void run() {
file.writeMessage(count, getName());
}
}

Output of the above program can result

MT1-1026
MT1-1027
MT1-1028
MT1-1029
MT1-1030
MT1-1031
MT1-1032
MT1-1MT2-0
MT2-1
MT2-2
MT2-3
MT2-4
MT2-5
MT2-6
MT2-7
MT2-8
MT2-9
MT2-10
MT2-11
MT2-12
MT2-13
MT2-14
MT2-15
MT2-16
MT2-17
MT2-18
MT2-19
033
MT1-1034
MT1-1035
MT1-1036
MT1-1037

Please note the bold lines carefully. In this case both thread is using same SampleFile Object and both thread is writing to a same file concurrently. Since there is no synchronized keyword for writeMessae() it is possible. if we make this method synchronized like below

public synchronized void writeMessage(int count, String message){
try {
FileWriter writer=new FileWriter(file,true);
for(int i=0;i
writer.write(message+"-"+i);
writer.write("\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}

then the output will change to like the following

MT1-9994
MT1-9995
MT1-9996
MT1-9997
MT1-9998
MT1-9999
MT2-0
MT2-1
MT2-2
MT2-3
MT2-4
MT2-5
MT2-6
MT2-7
MT2-8
MT2-9
MT2-10
MT2-11
MT2-12
MT2-13

Output 2 indicates that though both thread is started running the second thread is waiting until first thread completes its operation. though it is providing desired output we may not want to make the second thread wait until full completion of thread 1. This can be achieved by changing the program to following ways.


package org.vijayan.sample;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


public class SampleFile {

private FileWriter writer;
public SampleFile(String fileName){
File file=new File(fileName);
file.delete();
try {
writer=new FileWriter(file,true);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void finalize() throws Throwable {
super.finalize();
writer.close();
}
public void writeMessage(int count, String message){
try {
for(int i=0;i
synchronized (writer) {
writer.write(message+"-"+i);
writer.write("\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Output:

MT1-1218
MT1-1219
MT2-0
MT2-1
MT2-2
MT2-3
MT2-4
MT2-5
MT2-6
MT2-7
MT2-8

Please note adding a synchronized block requires some extra code and it always needs to be done carefully.

1 comment:

N P R said...

Nice example for synchronization.