An Unsynchronized Example Locking an Object

J.E.D.I.

9.5 Synchronization

So far, youve seen examples of threads that are running concurrently but are independent of each other. That is, threads run at their own pace without concern for the status and activities of the other concurrently running threads. In this instances, each thread do not require any outside resources or methods and as a result, does not need to communicate with other threads. In many interesting situations, however, concurrently running threads may require outside resources or methods. Thus, there is a need to communicate with other concurrently running threads to know there status and activities. A popular example on this scenario is the Producer-Consumer problem. The problem involves two main objects, the producer and the consumer. The work of the producer is to generate values or streams of data that the consumer in turn would receive or consume.

9.5.1 An Unsynchronized Example

Let us first consider a simple code that simply prints out a sequence of strings in a particular order. Here is the program. 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; PrintStringsThreadString str1, String str2 { this.str1 = str1; this.str2 = str2; thread = new Threadthis; thread.start; } public void run { TwoStrings.printstr1, str2; } } class TestThread { public static void mainString args[] { new PrintStringsThreadHello , there.; new PrintStringsThreadHow are , you?; new PrintStringsThreadThank you , very much; } } The program is expected to print out the two arguments of the Runnable objects consecutively. The problem, however, is the invocation of the sleep method causes other threads to be executed when some other thread is not yet finished with the execution of the print method of the TwoStrings class. Here is a sample output. Hello How are Thank you there. Introduction to Programming II Page 127 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