If you don’t call the unlock() method at the end of the critical section, the other threads that are waiting for that block will be waiting forever, causing a deadlock situation.
Java code blocks free#
At the beginning of the critical section, we have to get the control of the lock using the lock() method.Īt the end of the critical section, we have to use the unlock() method to free the control of the lock and allow the other threads to run this critical section. When we want to implement a critical section using locks and guarantee that only one execution thread runs a block of code, we have to create a ReentrantLock object. The key to the example is in the printJob() method of the PrinterQueue class.
![java code blocks java code blocks](https://cdn.appmus.com/images/a6cc1dd84e5981e2c2d87083883695a5.jpg)
Thread 1: PrintQueue: Printing a Job during 5 seconds :: Time - Tue Jan 06 15:19: Thread 2: PrintQueue: Printing a Job during 5 seconds :: Time - Tue Jan 06 15:19: Thread 3: PrintQueue: Printing a Job during 2 seconds :: Time - Tue Jan 06 15:19: Thread 4: PrintQueue: Printing a Job during 2 seconds :: Time - Tue Jan 06 15:19: Thread 6: PrintQueue: Printing a Job during 5 seconds :: Time - Tue Jan 06 15:19: Thread 5: PrintQueue: Printing a Job during 7 seconds :: Time - Tue Jan 06 15:19: Thread 7: PrintQueue: Printing a Job during 9 seconds :: Time - Tue Jan 06 15:19: Thread 8: PrintQueue: Printing a Job during 8 seconds :: Time - Tue Jan 06 15:19: Thread 9: PrintQueue: Printing a Job during 1 seconds :: Time - Tue Jan 06 15:19: Thread 0: PrintQueue: Printing a Job during 8 seconds :: Time - Tue Jan 06 15:19: Thread = new Thread(new PrintingJob(printerQueue), "Thread " + i) PrinterQueue printerQueue = new PrinterQueue() ("%s: The document has been printed\n", Thread.currentThread().getName()) (Thread.currentThread().getName() + ": PrintQueue: Printing a Job during " + (duration / 1000) + " seconds :: Time - " + new Date()) Long duration = (long) (Math.random() * 10000) Private final Lock queueLock = new ReentrantLock() A lock is maintained by printer to start new print job as soon as current print job is finished.
![java code blocks java code blocks](http://shebang.pl/wp-content/uploads/codeblocks.png)
This class represent the printer queue/ printer. ("%s: Going to print a document\n", Thread.currentThread().getName()) Public PrintingJob(PrinterQueue printerQueue) This class implements Runnable interface, so that printer can execute it when it’s turn come. This class represents an independent printing which could be submitted to printer. Once printer is done with print job in hand, it will pick another job from queue and start printing. Rest of jobs will wait there for their turn. Printer will take a job from printer queue and print it. You can submit a number of print jobs to printer during varying time interval or simultaneously. In this example, program will simulate the behavior of a printer. A Lock can have it’s calls to lock() and unlock() in separate methods. Using yLock(long timeout, TimeUnit timeUnit), it is possible.Ģ) The synchronized block must be fully contained within a single method. The main differences between a Lock and a synchronized block are:ġ) Having a timeout trying to get access to a synchronized block is not possible. Difference between Lock Interface and synchronized keyword Finally unlock() is called, and the Lock is now unlocked so other threads can lock it. Any other thread calling lock() will be blocked until the thread that locked the lock calls unlock(). Here is the simple use of Lock interface.įirst a Lock is created. ReentrantLock is one such implementation of Lock interface. Since Lock is an interface, you need to use one of its implementations to use a Lock in your applications. A Lock is, however, more flexible and more sophisticated than a synchronized block.
![java code blocks java code blocks](https://cdn.programiz.com/cdn/farfuture/ETPdbL9iEV_jJsMQVbbb4KZQTyJhPk_sfoiu7dbZAtk/mtime:1602652645/sites/tutorial2program/files/java-switch-case-implementation.png)
Lock InterfaceĪ .Lock is a thread synchronization mechanism just like synchronized blocks. In this tutorial, we will see a basic usage of Lock interface to solve printer queue problem. Java provides another mechanism for the synchronization of blocks of code based on the Lock interface and classes that implement it (such as ReentrantLock). We are already aware of basic concepts around thread synchronization and various mechanisms using synchronized keyword.