Is it possible to access two synchronized methods of a class at the same time

Is it possible to access two synchronized methods of a class at the same time – 

lock

Suppose a class (say class Book) has two ‘synchronized’ methods, then is it possible to access each of these ‘synchronized’ methods by two separate threads –

 

Now it is possible that two different threads can try access these two methods addBook() and updateBook() at the same time.

If the synchronized method is a static method, a class lock is used.

Synchronizing an instance method uses the object’s lock, here we are modifying the state of a specific object so there will be a object lock. If there is a lock on a object then no other thread can acquire lock on that object. So only one synchronized method can be called by a thread at a time on object.

If you use a synchronized block within the methods, they will run at the same time.

Gopal Das
Follow me

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

Leave a Reply

Your email address will not be published. Required fields are marked *