Movatterモバイル変換


[0]ホーム

URL:


Open In App

Every class in Java has a unique lock which is nothing butclass level lock.  If a thread wants to execute a static synchronized method, then the thread requires a class level lock. Class level lock prevents multiple threads to enter a synchronized block in any of all available instances of the class on runtime. This means if in runtime there are 10 instances of a class, only one thread will be able to access only one method or block of any one instance at a time. It is used if you want to protect static data.

If a thread wants to execute a static synchronized method, then the thread requires a class level lock. Once a thread got the class level lock, then it is allowed to execute any static synchronized method of that class. Once method execution completes automatically thread releases the lock.

Methods:Thread can acquire the lock at a class level by two methods namely

  1. Using the synchronized static method.
  2. Using synchronized block.

Method 1:Using thesynchronized static method

Implementation: We have a Geek class. We want to use static synchronization method of this class, as soon as the thread entered the synchronized method, the thread acquires the lock at the class level, rest of the threads wait to get the class monitor lock. The thread will leave a lock when it exits from the synchronized method.

public static synchronized int incrementCount(){}

Example

Java
// Java program to illustrate class level lock// Main Class// Implememnting the Runnable interfaceclassGeekimplementsRunnable{// Method 1// @Overridepublicvoidrun(){Lock();}// Method 2// Method is staticpublicstaticsynchronizedvoidLock(){// Gwetting the name of current thread by using// getName() method to get name of the thread and// currentThread() to get the current threadSystem.out.println(Thread.currentThread().getName());// class level locksynchronized(Geek.class){System.out.println("in block "+Thread.currentThread().getName());System.out.println("in block "+Thread.currentThread().getName()+" end");}}// Method 3// Main driver methodpublicstaticvoidmain(String[]args){// Creating an object of above class// in the main() methodGeekg1=newGeek();// Sharing the same object across two Threads// Creating an object of thread class where// t1 takes g1Threadt1=newThread(g1);// Creating an object of thread class where// t2 takes g1Threadt2=newThread(g1);// Creating second object of above class// in the main() methodGeekg2=newGeek();// Creating an object of thread class where// t3 takes g2Threadt3=newThread(g2);// setName() method is used to set name to the// threadt1.setName("t1");t2.setName("t2");t3.setName("t3");// start() method is used for initiating the current// threadt1.start();t2.start();t3.start();}}

 
 


Output
t1in block t1in block t1 endt3in block t3in block t3 endt2in block t2in block t2 end


 

Output explanation: 


 

Thread t1 entered the static synchronized method and was holding a lock on Geek's class. So, the rest of the threads waited for Thread t1 to release the lock on Geek's class so that it could enter the static synchronized method.


 

Method 2:Using synchronized block method 


 

Implementation: We have a "Geek" class. We want to create a synchronization block and pass class name. class as a parameter tells which class has to synchronized at the class level. As soon as the thread entered the synchronized block, the thread acquire the lock at class, rest of the threads wait to get the class monitor lock. The thread will leave lock when it exits from the synchronized block.


 

synchronized (Geek.class) {    //thread has acquired lock on  Geek class}


 

Example


 

Java
// Java program to illustrate class level lock// Main Class// It is implementing the Runnable interfaceclassGeekimplementsRunnable{// Method 1// @Overridepublicvoidrun(){// Acquire lock on .class referencesynchronized(Geek.class)// ClassName is name of the class containing method.{{System.out.println(Thread.currentThread().getName());System.out.println("in block "+Thread.currentThread().getName());System.out.println("in block "+Thread.currentThread().getName()+" end");}}// Method 2// Main driver methodpublicstaticvoidmain(String[]args){// Creating an object of above class// in the main() methodGeekg1=newGeek();// Creating an object of thread class i.e Thread// 1 where t1 takes g1 objectThreadt1=newThread(g1);// Here, creating Thread 2 where t2 takes g1// objectThreadt2=newThread(g1);// Creating another object of above class// in the main() methodGeekg2=newGeek();// Now Creating Thread 3 where t3 takes g2 objectThreadt3=newThread(g2);// Ginving custom names to above 3 threads// using the setName() methodt1.setName("t1");t2.setName("t2");t3.setName("t3");// start() method is used to begin execution of// threadst1.start();t2.start();t3.start();}}

  

Output:


 

t1in block t1in block t1 endt3in block t3in block t3 endt2in block t2in block t2 end


 

Output explanation: 


 

Thread t1 entered synchronized block and was holding the lock on 'Geek' class. So, the rest of the threads waited for Thread t1 to release the lock on the 'Geek' class so that it could enter the synchronized block.


 


Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp