Synchronized Block in JavaSynchronized block can be used to perform synchronization on any specific resource of the method. Suppose we have 50 lines of code in our method, but we want to synchronize only 5 lines, in such cases, we can use synchronized block. If we put all the codes of the method in the synchronized block, it will work same as the synchronized method. Points to Remember
SyntaxExample of Synchronized BlockLet's see the simple example of synchronized block. TestSynchronizedBlock1.java Output: 5 10 15 20 25 100 200 300 400 500 Synchronized Block Example Using Anonymous ClassTestSynchronizedBlock2.java Output: Sending a Message: Hola Hola Sent Sending a Message: Welcome to Javatpoint website Welcome to Javatpoint website Sent Next TopicStatic Synchronization Example |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India