Javarevisited Newsletter

Javarevisited Newsletter

Share this post

Javarevisited Newsletter
Javarevisited Newsletter
Difference between Lock, synchronized, and Semaphore in Java

Difference between Lock, synchronized, and Semaphore in Java

javinpaul's avatar
javinpaul
Mar 23, 2024
∙ Paid
6

Share this post

Javarevisited Newsletter
Javarevisited Newsletter
Difference between Lock, synchronized, and Semaphore in Java
1
2
Share

Hello guys, how are you doing? In the past few episodes, I have shared popular Java interview questions like How ConcurrentHashMap work in Java? Why String is Immutable and What is the difference between List, List<Object>, and List<?> Today I am going to share another popular Java interview question, what is the difference between synchronized, Lock, and Semaphore in Java?

My goal is to share two Java interview questions every week so that you are always ready to take your next Java interview, rather than doing last-minute preparation or losing your first few interviews because you are not warmed up. If your goal is the same then please consider getting a paid subscription as these questions will only be for paid subscribers.

Coming back to the topic, In concurrent programming, managing access to shared resources is paramount to ensuring correctness and efficiency. In Java, developers have several mechanisms for synchronizing access among multiple threads. Among the most commonly used are synchronized, Lock, and Semaphore. Each of these mechanisms offers distinct features and trade-offs, catering to different concurrency scenarios.

For example, synchronized is a language-level construct in Java used for mutual exclusion, Lock provides more flexibility with explicit locking and unlocking, while Semaphore allowing controlling access to shared resources through a set of permits, enabling multiple threads to access simultaneously up to a defined limit.

Later we will also see a real-world example of where exactly you can use synchronized, Lock and Semaphore in Java.

Keep reading with a 7-day free trial

Subscribe to Javarevisited Newsletter to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 javinpaul
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share