That is, these 20 lines can alter or change the state of the object. Suppose a method consists of500 LOC(lines of code), but there exist only 20 lines of code that holds a critical section(CS) of code. Similarly, synchronized keyword assures that at a time, only 1 thread will be executing the lines of code in a synchronized block, which in turn prevents more than one thread from corrupting the data which is shared within the block. Each thread has to wait until the thread that currently holds the lock completes the execution and releases it. At a time, only 1 thread can acquire the lock object’s monitor. When a thread needs to execute lines that are synchronized inside the synchronized block, it is mandatory to acquire the lock on the monitor of the lock Object lo mentioned in the syntax above. Here, lo is the lock object How does Synchronized Block Work in Java?Īs already discussed, the Synchronized block helps in performing synchronization on any particular resource of the function or method. statements that have to be synchronized The syntax of the synchronized block is as shown below: Web development, programming languages, Software testing & others Start Your Free Software Development Course
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |