Can two threads call two different synchronized instance methods of an Object?

Carvia Tech | December 16, 2017 | 1 min read | 166 views


No. If a object has synchronized instance methods then the Object itself is used a lock object for controlling the synchronization. Therefore all other instance methods need to wait until previous method call is completed.

See the below sample code which demonstrate it very clearly. The Class Common has 2 methods called synchronizedMethod1() and synchronizedMethod2() MyThread class is calling both the methods.

public class Common {
  public synchronized void synchronizedMethod1() {
   System.out.println("synchronizedMethod1 called");
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   System.out.println("synchronizedMethod1 done");
  }

  public synchronized void synchronizedMethod2() {
   System.out.println("synchronizedMethod1 called");
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
 }

Top articles in this category:
  1. What will happen if we don't synchronize getters/accessors of a shared mutable object in multi-threaded applications
  2. What are four principles of OOP, How aggregation is different than Composition?
  3. What is difference between sleep() and wait() method in Java?
  4. What is Immutable Class. Why would you choose it? How would you make a class immutable?
  5. Producer Consumer Problem using Blocking Queue in Java
  6. What is difference between Callable and Runnable Interface?
  7. What is ThreadLocal in Java, where will you use this class



Find more on this topic:
Core Java image
Core Java

Core Java - OOP Concepts, Garbage Collection, Multi-threading, Collections Framework, Java 8 Features, Lambda Functions, Streams.

Last updated 1 month ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info