J.E.D.I.
you? very much
In this run, the three threads got to have their first string argument printed before having their second argument printed. This results in a cryptic output.
Actually, the example here doesnt pose a very serious problem but for other applications, this may cause some exceptions or problems to occur.
9.5.2 Locking an Object
To assure that only one thread gets to access a particular method, Java allows the locking of an object including its methods with the use of monitors. The object enters the
implicit monitor when the objects synchronized method is invoked. Once an object is in the monitor, the monitor makes sure that no other thread accesses the same object. As a
consequence, this ensures that only one thread at a time gets to execute the method of the object.
For synchronizing a method, the synchronized keyword can be prefixed to the header of the method definition. In the case that you cannot modify the source code of the
method, you can synchronize the object of which the method is a member of. The syntax for synchronizing an object is as follows:
synchronized object { statements to be synchronized
}
With this, the objects methods can only be invoked by one thread at a time.
9.5.3 First Synchronized Example
Here now is the modified code wherein the print method of the TwoStrings class is now synchronized.
class TwoStrings { synchronized static void printString str1, String str2 {
System.out.printstr1; try {
Thread.sleep500; } catch InterruptedException ie {
} System.out.printlnstr2;
} }
class PrintStringsThread implements Runnable { Thread thread;
String str1, str2; PrintStringsThreadString str1, String str2 {
this.str1 = str1; this.str2 = str2;
thread = new Threadthis; thread.start;
} public void run {
TwoStrings.printstr1, str2;
Introduction to Programming II Page 128
J.E.D.I.
} }
class TestThread { public static void mainString args[] {
new PrintStringsThreadHello , there.; new PrintStringsThreadHow are , you?;
new PrintStringsThreadThank you , very much; }
}
The program now produces the correct statements. Hello there.
How are you? Thank you very much
9.5.4 Second Synchronized Example
Here is still another version of the preceding code. Again, the print method here of the TwoStrings class is now synchronized. But, instead of applying the synchronized keyword
to a method, it is applied to an object instead.
class TwoStrings { static void printString str1, String str2 {
System.out.printstr1; try {
Thread.sleep500; } catch InterruptedException ie {
} System.out.printlnstr2;
} }
class PrintStringsThread implements Runnable { Thread thread;
String str1, str2; TwoStrings ts;
PrintStringsThreadString str1, String str2, TwoStrings ts {
this.str1 = str1; this.str2 = str2;
this.ts = ts; thread = new Threadthis;
thread.start; }
public void run { synchronized ts {
ts.printstr1, str2; }
} }
class TestThread { public static void mainString args[] {
TwoStrings ts = new TwoStrings; new PrintStringsThreadHello , there., ts;
new PrintStringsThreadHow are , you?, ts; new PrintStringsThreadThank you , very much, ts;
Introduction to Programming II Page 129
J.E.D.I.
} }
This program also outputs the correct statements.
Introduction to Programming II Page 130
J.E.D.I.
9.6 Interthread Communication