Category: Java tutorial
-
Why is CompletableFuture Preferred Over Plain Threads in Microservices?
In modern microservice architectures, asynchronous programming plays a critical role in building scalable, responsive, and resource-efficient systems. While Java allows you to create threads using the Thread class or Runnable interface, using CompletableFuture is often the preferred approach in microservices. This tutorial explains why CompletableFuture is preferred over plain threads with in-depth comparisons, real-world use…
-
Difference Between volatile and AtomicReference in Java
1. Introduction to volatile 1.1. Definition The volatile keyword in Java is used to mark a variable as being shared among threads. It ensures that all reads and writes to that variable go directly to and from main memory (RAM), not CPU cache. 1.2. Key Properties 1.3. Syntax Although count is volatile, count++ is not…
-
How Does Java Handle False Sharing in Concurrency?
1. Introduction In modern multi-threaded Java applications, performance issues may arise from subtle low-level hardware behavior. One such issue is false sharing — a CPU cache-related performance bottleneck. This tutorial explores what false sharing is, how it impacts Java concurrency, and how Java handles or mitigates it, especially through memory padding and Java 8+ features.…
-
ScheduledThreadPoolExecutor in Java
1. What is a ScheduledThreadPoolExecutor? ScheduledThreadPoolExecutor is a part of Java’s java.util.concurrent package. It allows you to schedule tasks to run after a delay or periodically, using a pool of threads. It is a powerful replacement for the older Timer and TimerTask classes, offering: 2. Where is it defined? It implements: 2. Why use ScheduledThreadPoolExecutor…
-
Fair vs Non-Fair Locks in Java
1. What is a Lock? A lock is a synchronization mechanism that allows only one thread at a time to access a critical section (shared resource or code block). Java provides various lock implementations in the java.util.concurrent.locks package, especially the ReentrantLock class, which supports fair and non-fair locking policies. 2. Fair Lock 2.1 What is…
-
Synchronized Block vs Synchronized Method
In multithreaded programming, multiple threads may try to read and modify shared data at the same time, leading to data corruption, inconsistent results, or unexpected behavior. To solve this, Java provides a built-in mechanism called synchronization to control access to critical sections of code. Java offers two primary ways to synchronize code: Both are used…
-
Comparison of newCachedThreadPool() vs newFixedThreadPool()
1. What is a Thread Pool? A thread pool is a group of pre-instantiated reusable threads that are maintained to execute tasks. Instead of launching a new thread every time a task arrives (which is costly in terms of time and system resources), tasks are submitted to a queue, and threads from the pool handle…
-
Why use Virtual Threads?
Use virtual threads in high-throughput concurrent applications, especially those that consist of a great number of concurrent tasks that spend much of their time waiting. Server applications are examples of high-throughput applications because they typically handle many client requests that perform blocking I/O operations such as fetching resources. Virtual threads are not faster threads; they…
-
PriorityBlockingQueue in Java
The PriorityBlockingQueue in Java is a thread-safe, unbounded implementation of the BlockingQueue interface. It orders elements according to their natural ordering or by a Comparator provided at construction time. Unlike PriorityQueue, it’s designed for use in concurrent environments, where multiple threads might be adding and removing elements simultaneously. Key Characteristics Creating a PriorityBlockingQueue You can…
-
PriorityQueue in Java
A PriorityQueue in Java is a special type of queue where elements are ordered based on their natural ordering or a custom comparator, not by the order in which they were added. The element with the highest priority is always at the front of the queue. Key Characteristics Creating a PriorityQueue You can instantiate a…
