If I synchronized two methods on the same class, can they run simultaneously?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In the realm of concurrent programming in Java, understanding synchronization is crucial to ensuring the correct functioning of applications. One common point of confusion pertains to synchronizing methods within a class: if two methods are synchronized on the same object, can they execute concurrently?
Understanding Java Synchronization
Java provides the synchronized keyword to control access to methods or blocks of code that are critical sections, meaning they should not be accessed by more than one thread at a time. Synchronization in Java is based on monitors or intrinsic locks. Every object in Java has an intrinsic lock associated with it, and a thread needs to obtain this lock to enter a synchronized block or method.
Key Concepts
- Synchronized Methods: When you declare a method as
synchronized, the thread invoking this method must acquire the intrinsic lock associated with the object owning the method. - Lock Contention: If one thread holds the lock, other threads attempting to enter any of the object's synchronized methods will block until the lock is released.
- Atomicity and Visibility: Synchronization ensures that a method or block of code is executed atomically, and it also ensures memory visibility so that changes made in one thread are visible to others.
Can Two Synchronized Methods Run Concurrently?
Same Object Synchronization
When two methods are synchronized on the same object (typically the instance), the rule is straightforward:
- If both methods are instance methods and are synchronized on the same object (the current instance or
this), they cannot run simultaneously.
The intrinsic lock acquired on entering a synchronized method is the same lock released on exiting the method. Thus, if one method holds the lock, another method that attempts to acquire the same lock must wait.
In this setup, if method1() is executing, method2() cannot start, as both require the same lock.
Different Object Instances
Synchronized methods in different instances of the same class do not interfere with each other. Each object has its own intrinsic lock.
Here, each thread is working with a separate instance, allowing concurrent execution of method1() on instance1 and method2() on instance2.
Static Synchronization
Static methods synchronize on the class object, not the instance. Thus, all static synchronized methods in a class can block each other:
Static methods staticMethod1() and staticMethod2() use the class object to synchronize, so they can't run simultaneously.
Summary Table
| Condition | Concurrent Execution Allowed? |
| Same instance, two synchronized methods | No |
| Different instances, methods on each | Yes |
| Static synchronized methods on a class | No |
Additional Considerations
- Reentrant Lock: Java also provides
java.util.concurrent.locks.ReentrantLockas an alternative to intrinsic locks for more advanced control over synchronization. - Performance Considerations: Overuse of synchronization can lead to performance bottlenecks due to increased contention.
- Read vs. Write Locks: For collections or shared data that may involve frequent reads and infrequent writes,
java.util.concurrent.locks.ReadWriteLockcan improve performance.
In conclusion, understanding how synchronization works in Java is key to developing robust multithreaded applications. Synchronizing methods carefully and understanding the implications can prevent several concurrency issues like race conditions and deadlocks.
Related reading
- If I synchronized two methods on the same class, can they run simultaneously?
- If I want to give more work to my Process Pool, can I call Pool.join before Pool.close?
- If Javascript is single threaded, how things like a clock work?
- if main method completes the execution, what happens to any long running thread?
- Ignore duplicates when producing map using streams
- Illegal pattern character 'T' when parsing a date string to java.util.Date
- If my interface must return Task what is the best way to have a no-operation implementation?
- If threads share the same PID, how can they be identified?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.