There is a class EmployeeInfo it has a static synchronized method non static synchronized method
public class EmployeeInfo{
public static synchronized void insert(){
//Insert code
}
public synchronized void update(){
//update code
}
public void delete(){
//update code
}
}
There are five threads A, B, C, D, and E. Every thread is running.
A thread comes and accesses the method insert()
of class EmployeeInfo
.
At the same time:
insert()
method again - what will happen? update()
- what will happen? delete()
- what will happen? Please explain the concept of class level synchronization following the above example.
There are two separate locks involved here - one for the instance on which you call update
, and one for the class itself. So thread B would be blocked until thread A had completed, but the other two threads would execute without blocking. (D isn't synchronizing on anything anyway.)
Your code is broadly equivalent to:
public class EmployeeInfo{
public static void insert(){
synchronized (EmployeeInfo.class) {
// Insert code
}
}
public void update() {
synchronized (this) {
// Update code
}
}
public void delete() {
// Note: no synchronization
// Delete code
}
}
I don't completely understand your question, but it looks as if you might be asking this:
If the class is locked by one thread [can other threads access] that class?
Absolutely! "Lock" is a deceptive word. When you use the synchronized
keyword to lock an object (including a class object) it does not prevent other threads from accessing or modifying the object.
The only thing that synchronized
does is it prevents two threads from synchronizing on the same object at the same time. If you want to prevent two or more threads from operating on the same data at the same time, then it is your responsibility to ensure that every method that can access the data is synchronized on the same object when it does it.
Class level locking prevents multiple threads to enter in synchronized block in any of all available instances on runtime.As per example for insert method of class EmployeeInfo
I created object of class EmployeeInfo .
EmployeeInfo e1=new EmployeeInfo(); Thread A comes and access insert() method of e1.
Now again created a new object for class EmployeeInfo EmployeeInfo e2=new EmployeeInfo(); Thread B comes and access insert() method of e2.
Now again created a new object for class EmployeeInfo EmployeeInfo e3=new EmployeeInfo(); Thread C comes and access insert() method of e3.
Currently thread A has monitor for EmployeeInfo object thread B and C both can't access insert() method of EmployeeInfo object while i created different-2 instances for EmployeeInfo object e1,e2,e2,Because here lock is on class level. So my statement giving a perfect example to understand class level synchronization . "Class level locking prevents multiple threads to enter in synchronized block in any of all available instances on runtime".
Inspite of that if we use Object level syncronization then then lock is on only one instance.not for every object e2,e3,e4.So lock will be for only specific object which is locked by anyone thread.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.