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 –

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 –

Java

1

2

3

4

5

6

7

8

9

10

11

ClassBook{

publicsynchronizedintaddBook(Book book){

//code block

}

publicsynchronizedintupdateBook(Book book){

//code block

}

}

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.

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.