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.
Now let’s start with one simple multi-threaded application. Let’s say there are two threads
Writerand both are sharing a common object
Readerreads the message when it is not empty otherwise it will wait for the
Writerto write the
Writerwrites the message…
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
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.
Basically, HashMap is one of the most popular Collection classes in…
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.
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.
In this article, I’ll be covering two important keywords synchronized and volatile, and their proper usage. …
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.
1. The Callable interface is similar to Runnable, both are designed for classes whose instances are potentially executed by another thread.
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.
“Define a one-to-many dependency between objects so that…
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
For synchronization using the
synchronized keyword, you may refer to this article: Synchronization in Java: All You Need to Know
This article is all about Deadlock, we’ll see a deadlock program and will find an approach to overcome the deadlock condition.
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).
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.
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…
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…