Movatterモバイル変換


[0]ホーム

URL:


Open In App

Every object in Java has a unique lock. Whenever we are using a synchronized keyword, then only the lock concept will come into the picture. An object-level lock is a mechanism when we want to synchronize a non-static method or non-static code block such that only one thread will be able to execute the code block on a given instance of the class. 

If a thread wants to execute a synchronized method on the given object. First, it has to get a lock of that object. Once the thread gets the lock then it is allowed to execute any synchronized method on that object. Once method execution completes automatically thread releases the lock. Acquiring and releasing lock internally is taken care of by JVM and the programmer is not responsible for these activities.

Methods

There are different ways we can lock the object in the thread as below:

Method 1:

public class GeekClass
{
public synchronized void GeekMethod(){}
}

Method 2:

public class GeekClass
{
public void GeekMethod(){
synchronized (this)
{
// other thread safe code
}
}
}

Method 3:

public class DemoClass
{
private final Object lock = new Object();
public void demoMethod(){
synchronized (lock)
{
// other thread safe code
}
}
}

Example of Object Level Lock

Below is the implementation of Object Level Lock is mentioned below:

Java
// Java program to illustrate// Object lock concept// Class// Extending Runnable interfaceclassGeekimplementsRunnable{// Method of this classpublicvoidrun(){Lock();}// Synchronization of non-static methods// (object lock) as different synchronized// non-static methods are called in both threads// Then both threads need to acquire the object lock// After one is acquired, the other thread must wait// for one thread to finish the executing// before the other thread starts to execute.publicvoidLock(){System.out.println(Thread.currentThread().getName());synchronized(this){System.out.println("in block "+Thread.currentThread().getName());System.out.println("in block "+Thread.currentThread().getName()+" end");}}// Main driver methodpublicstaticvoidmain(String[]args){// Creating an object of above class// in the main() methodGeekg=newGeek();// Sharing the same object across two Threads// Here, t1 takes gThreadt1=newThread(g);// Here, t2 takes gThreadt2=newThread(g);// Creating another object of above classGeekg1=newGeek();// Here, t3 takes g1Threadt3=newThread(g1);// setname() method is used to change// name of the threadt1.setName("t1");t2.setName("t2");t3.setName("t3");// start() method beginning the execution of threads// as JVM calls the run() method of threadt1.start();t2.start();t3.start();}}

Output
t1t2t3in block t1in block t3in block t3 endin block t1 endin block t2in block t2 end


 


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