Sign in

| Software Developer | Programming enthusiast | Loves DS and Algo | Passionate about Future | Writes Mostly tech stuff |

A better way to handle synchronization than synchronized keyword.

Producer and Consumer image/author

We have already learned how to synchronize a shared resource using a synchronized keyword. Now in this article, we’ll see a better way to synchronize shared resource using ReentrantLock.

For synchronization using the synchronized keyword, you may refer to this article: Synchronization in Java: All You Need to Know

Problems With Synchronized Keyword:

  • A thread can only acquire a lock on an object only once for the shared resources.
  • Once the thread completes the execution of the synchronized block, there is no mechanism by which the other threads will get the lock.
  • Let’s take a scenario and understand the starvation issue with multiple threads…

Get prepared for multi-threading interview questions.

Deadlock on a single lock

Understanding multithreading in any language is a bit tough as it involves many other concepts like thread-stack, heap, locking, synchronization, etc. Earlier I have tried and attempted to simplify the multithreading concepts with multiple articles.

If you haven't gone through those articles, here is one article to start with.

Multi-Threaded Application:

Now let’s start with one simple multi-threaded application. Let’s say there are two threads Readerand Writerand both are sharing a common object “message”.

  1. Reader reads the message when it is not empty otherwise it will wait for the Writer to write the message.
  2. On the other hand, Writer writes the message…

Know your next interview question.

HashMap with Singly Linked List

I have seen a lot of interviewers asking for the internal working mechanism of the hashmap. If you know the complete internal working of one of the map implementations, it becomes easier for you to understand the other classes of the map interface.

So thru this article, we’ll see how the hashing is done and its complete working mechanism, how the search, insert and removeoperations work with the hashmap and their time complexities, and at the end, we’ll see why it should not be used in a multi-threading environment.

What is HashMap?

Basically, HashMap is one of the most popular Collection classes in…

Java synchronization guide for your next interview…

Photo by Max Duzij on Unsplash

Through this article we are going to discuss atomic variables in java, why and where are they used, and how synchronized, volatile and atomic are different. Okay, okay don’t get confused, I’ve already covered synchronization and volatile in detail.

But before going ahead, you should know Race Condition, Critical Section, and Context Switching between threads. I have already covered these concepts and their examples in detail in the below articles. You should go thru them to get a complete and clear understanding of these concepts. Anyway, after reading these articles you will realize multi-threading is not that difficult.

What Does Atomic mean?

int x=…

Don’t misuse synchronized and volatile in your application.

Cache Coherence

I have seen many developers a bit confused when it comes to multi-threading. This is primarily because a lot of things happen in the background and many concepts are related to it. You should know Race Condition, Critical section, and Context Switching between threads. I have already covered these concepts and their examples in detail.

Process and Thread Context Switching, Do You Know the Difference?

Synchronization in Java: All You Need to Know

In this article, I’ll be covering two important keywords synchronized and volatile, and their proper usage. …

Clear deadlock concept with an example.

This article is all about Deadlock, we’ll see a deadlock program and will find an approach to overcome the deadlock condition.

What Is a Deadlock?

Deadlock occurs when multiple threads need the same locks but obtain these locks in a different order. A Java multithreaded program may suffer from the deadlock condition because the synchronized keyword causes the executing thread to block while waiting for the lock associated with the specified object.

Deadlock is a condition where the threads are waiting infinitely for the resources(locks).

Now Let’s Write a Program and See What’s the Issue With This Program.

There are two threads in the program and two locks are created. Each thread needs both locks to execute…

Expect this question in your multi-threading interviews.


I really love reading and exploring multi-threaded problems and share them with my friends and colleagues. I’m always in the favor of learning something until you understand it completely. Keeping that in mind, I’m writing this article for all those who are trying to understand and learn multi-threading. I have already written few articles on this topic that I will list at the bottom for your reference. In this article, we’ll understand one classical multi-threading problem.

A Small Background of Producer-Consumer Problem:

The Producer-Consumer Problem (sometimes called the Bounded-Buffer Problem) is a classic example of a multi-threaded synchronization problem.

The problem describes two threads, the Producer…

Get to know the race condition, thread-stack, and heap memories with examples.

Photo by Max Duzij on Unsplash

Thread Interference or Race Condition is one of the most asked multi-threading interview questions. When you start learning multi-threading, your focus should be initially on the basics rather than going for the tough concepts. Before knowing anything, you must know how to create threads using Thread class, Runnable interface, and Anonymous class thread.

Once you are clear with creating and running your own thread you may proceed with a bit more complex concepts like Thread Interference, Deadlocks, Thread Synchronization, Locking mechanism, ArrayBlockingQueue, Thread Starvation, etc.

Through this article, I’ll be explaining few important concepts like thread-stack, heap memory, and then…

Get ready for the HashSet interview question.

HashSet and HashMap design

While going thru the Java interview questions on the collection framework, I have seen a lot of interviewers asking for the internal working mechanism of the hashmap. Okay, you may be knowing that I have already covered that in detail. But in some of the interviews, the interviewer might ask you about HashSet instead of HashMap. If you have gone thru my HashMap article then it would become very easy for you to understand the working of HashSet. Because internally HashSet is backed by a HashMap.

What Is HashSet?

HashSet is one of the data structures in the Java Collections framework.

Let’s see…

Know all the differences and comparisons and rock your interview.

Doubly linked list

Hey, I’m back with another article on the Collections framework. While ago when I was preparing for the interviews, I find this very common question in all of the interviews. If you search on the web, you’ll find many answers, but most of them are just writing basic differences that anyone can answer.

When I was giving interviews my focus was not just to tell the technical difference but craft my answer on a use case driven. I presented the answers with their use cases and along with that explained the differences.

Through this article, you will find a better…

Vikram Gupta

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store