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
starvationissue with multiple threads…
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. …
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…
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.
HashSet is one of the data structures in the Java Collections framework.
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…