Sign in

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

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. …

Know your next java interview question.

Most Frequently asked java multithreading interview questions.

Photo by Nico Nazaire on Unsplash

Multithreading is one of the core concepts in any programming language. Understanding concurrency and threading take some time and prepare yourself in a way to clear your multithreading interviews. If you follow this list, which contains the most frequently asked multithreading questions, you may take less time to prepare for the interviews.

Runnable is the core interface provided for representing multi-threaded tasks and implementing threads and Callable is an improvised version of Runnable. Callable was added in Java 1.5.

Just Two Statements:

1. The Callable interface is similar to Runnable, both are designed for classes whose instances are potentially executed by another thread.

A behavioral design pattern for Flexible Notification-Registration Interaction.

Observer design pattern architecture

In this article, we will see what is the intent of the Observer design pattern, what problems it solves, and its applicability.

Before we move to the technicality of this topic, let me give one of the most basic use cases of this design pattern. We mostly tend to subscribe to the Youtube channels that we love watching. So in this example, the channel is an example of a Subject and the users subscribing to it are Observers. Whenever there is an update on the channel all the subscribed users get notified.

The Intent of Observer Design Pattern Is To:

“Define a one-to-many dependency between objects so that…

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…

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…

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