![]() |
VOOZH | about |
Class level lock is a lock on the Class object, used when working with static synchronized methods or blocks. Only one thread can execute any static synchronized code of a class at a time, regardless of number of objects.
It is mainly used to protect shared static data.
This method automatically acquires the class-level lock when a thread enters it. Only one thread can execute any static synchronized method of the class at a time.
t1 in block t1 in block t1 end t3 in block t3 in block t3 end t2 in block t2 in block t2 end
This method uses a block where the class object is explicitly locked using ClassName.class. It allows better control by synchronizing only specific code.
Output:
t1
in block t1
in block t1 end
t3
in block t3
in block t3 end
t2
in block t2
in block t2 end
Output explanation: Thread t1 acquired the class lock by entering the synchronized block. Other threads waited until t1 released the lock