A Brief Introduction About The Lock in Java
Posted By : Nanda Ram | 30-Apr-2018
Every class in Java has a unique lock. If a thread wants to execute a static synchronized method then it is required class level lock. While a thread executing a static synchronized method then the remaining threads are not allowed to execute any static synchronized method of that class simultaneously but remaining threads are allowed to execute the following methods simultaneously
1. Normal static methods
2. Normal instance methods
3. Synchronized instance methods
Object level lock
Every object in Java has a unique lock. If a thread wants to execute a synchronized method then it is required object level lock. Once the thread gets the lock on that object only that thread is allowed to execute that method rest of the threads have to wait until the current thread is not releasing the lock.
the lock will release once the method gets completed then a thread will release the lock.
Note:
There is no link between object level lock and class level lock but both are independent of each other.
A class level lock is different and object level lock is different.
Synchronized Block
If very few lines of code need synchronization then it is never to declare the whole method as synchronized. we have to declare those few lines of code inside a synchronized block.
Case 1:
We can declare the synchronized block to get current object lock as follows
synchronized(this)
{
System.out.println("Current thered Name " + Thread.currentThread().getName());
}
If thread got a lock of the current object then only it is allowed to execute this block otherwise not possible to execute this block.
Case 2:
To get a lock of an individual object we can declare synchronized block as follows
synchronized(a)
{
}
If thread got a lock of b object then that thread is allowed to execute this block.
Case 3:
To get class level block we can declare synchronized block as follows
synchronized(classname.class)
{
}
If thread got a class level lock of class name ( EX. display ) class then only it is allowed to execute that block.
Case 4:
Synchronized block is allowed with only for classes and object, not for primitive types data.
Int x=10;
synchronized( x)
{
}
C:E: unexpected type
found: int
required: reference
class ObjectLevelLockDemo implements Runnable {
public void run()
{
Lock();
}
public void Lock()
{
System.out.println("current thread name="+ Thread.currentThread().getName());
synchronized(this)
{
System.out.println("in synchronized block "+ Thread.currentThread().getName());
}
}
public static void main(String[] args)
{
ObjectLevelLockDemo g = new ObjectLevelLockDemo ();
Thread t1 = new Thread(g);
Thread t2 = new Thread(g);
ObjectLevelLockDemo g1 = new ObjectLevelLockDemo ();
Thread t3 = new Thread(g1);
t1.setName("t1");
t2.setName("t2");
t3.setName("t3");
t1.start();
t2.start();
t3.start();
}
}
class ClassLevelLockDemo implements Runnable {
public void run()
{
Lock();
}
public void run()
{
Lock();
}
public void Lock()
{
System.out.println("current thread name="+ Thread.currentThread().getName());
synchronized(Geek.class)
{
System.out.println("in synchronized block "+ Thread.currentThread().getName());
}
}
public static void main(String[] args)
{
ClassLevelLockDemo g1 = new ClassLevelLockDemo();
Thread t1 = new Thread(g1);
Thread t2 = new Thread(g1);
ClassLevelLockDemo g2 = new Test();
Thread t3 = new ClassLevelLockDemo(g2);
t1.setName("t1");
t2.setName("t2");
t3.setName("t3");
t1.start();
t2.start();
t3.start();
}
}
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Nanda Ram
Nanda Ram is a Java Developer, he keep conscientious about his task to complete it in a effective and efficient manner .