Capture and Analyze a Thread Dump in Java
Learn to capture and analyze thread dumps in Java either manually to using tools such as JStack, VisualVM, FastThread, TDA and JProfiler, etc.
Learn to capture and analyze thread dumps in Java either manually to using tools such as JStack, VisualVM, FastThread, TDA and JProfiler, etc.
Java concurrency interview guide lists frequently asked questions for beginners and experienced programmers to help refresh multi-threading concepts.
In Java, the volatile variables should be utilized when all changes made to a variable by one thread are immediately visible to other threads.
In Java, local variables are thread-safe because they are stored on the stack memory, which is a unique space allocated to each thread when it is created.
Learn about Java Runnable and Callable Interfaces with simple examples and some of the main usages and differences between both interfaces.
Learn what is a daemon thread in Java, how to create a daemon thread, and various methods present for daemon threads in the Thread class.
Learn what a Semaphore and a Reentrant Lock are along with practical examples. We will also explore some of the main differences between the two and the use cases where we can use these while working in a multithreaded application. 1. What is a Semaphore? A Semaphore is a Thread …
Learn IllegalMonitorStateException, when it is thrown with examples. We will also see how we can prevent our code from raising this exception
Learn the need and methods to join two threads in Java with examples. Learn to set wait expiration time and avoid thread deadlock conditions.
The backbone of Java concurrency are threads. A thread is a lightweight process with its own call stack but can access shared data of others.
Learn to kill a running thread in Java using a boolean flag and sending interrupt message. Sending an interrupt is a much better approach.
Learn the different ways to set a custom name for a thread using Thread constructor and setName(). Also, learn to get the name of a thread.
Java 21 Structured concurrency (JEP-428) aims to simplify concurrency by treating multiple tasks running in different threads as a single unit of work.
In Java, Virtual threads (JEP-425) are JVM managed light-weight threads that will help in writing high throughput concurrent applications.
DelayQueue class is an unbounded blocking queue of delayed elements, in which an element can only be taken when its delay has expired.
Learn ExecutorService shutdown(), shutdownNow() and awaitTermination() APIs and how to use them correctly to shutdown executor under different conditions,
Learn to cancel a task submitted to an executor service that still has to be executed not completed yet using Future.cancel() API.
Learn to execute a task after a period of time or execute it periodically using ScheduledExecutorService class in Java using ScheduledThreadPoolExecutor.
Learn to run multiple Callable tasks with ExecutorService.invokeAll(tasks) API and processing all the results returned from tasks in form of Future class instances in this ExecutorService Callable example.
Learn to use ExecutorService invokeAny() method where we execute multiple tasks at same time, but we make a decision asap any one of tasks is completed.
Learn to handle tasks which are submitted to Executor and are rejected because the executor has been shutdown for any reason using RejectedExecutionHandler. 1. When tasks get rejected Remember when we finish the execution of an executor, we use the shutdown() method. The executor waits for the completion of tasks …
Learn to use Java ExecutorService to execute a Runnable or Callable class in an asynchronous way. Also learn the various best practices to utilize it in most efficient manner in any Java application.
The AtomicInteger class protects an underlying int value by providing methods that perform atomic operations on the value. It shall not be used as a replacement for an Integer class.
Java Hashtable class is an implementation of hash table data structure. It is very much similar to HashMap but it is synchronized while HashMap is not.
BlockingQueue is excellent when you want to skip the complexity involved in wait–notify statements. This BlockingQueue can be used to solve the producer-consumer problem as well as given blow example. As this problem is well known to every programmer, I am not going in detail of problem description. How BlockingQueue …
Learn about the lifecycle of a Thread in Java, and how the state transitions happen between different states of the Thread by JVM and OS.
Concurrency means multiple tasks running in overlapping time periods. Parallelism is when several parts of a unique task run at the same time
One of our reader, Anant, asked this extremely good question to elaborate / list down all related topics that we should know about multi-threading including changes made in java 8.( Beginner level to Advance level). All he wanted to know was evolution of Multi-threading Framework in Java from Simple Runnable …
One of the benefits of the Java executor framework is that we can run concurrent tasks that may return a single result after processing the tasks. The Java Concurrency API achieves this with the following two interfaces Callable and Future. In this tutorial, we will learn to execute Callable tasks …
In previous tutorial, we learned about basic thread pool executor with unlimited possible number of threads into the pool and it’s example usage. Now lets look example of fixed size thread pool executor which will help in improved performance and better system resource utilization by limiting the maximum number of …
Why do we need a thread pool in Java? The answer is when we develop a simple, concurrent application in Java, we create some Runnable objects and then create the corresponding Thread objects to execute them. Creating a thread in Java is an expensive operation. And if you start creating …
In java, most used data structure is probably a list. A list has an undetermined number of elements and you can add, read, or remove the element of any position. Additionally, concurrent lists allow the various threads to add or remove elements in the list at a time without producing …
In concurrent programming, a semaphore is a synchronization aid that manages access to a shared resource by multiple threads.
The Object class in Java has three final methods that allow threads to communicate i.e. wait(), notify() and notifyAll(). Learn how to use these methods.
A binary semaphore is a synchronization primitive that can hold only two values, typically 0 and 1. We use binary semaphore to manage access to a shared resource by multiple threads in a concurrent environment. Conceptually, a binary semaphore can be thought of as a lock that allows only one …
Java provides another mechanism for the synchronization of blocks of code based on the Lock interface and classes that implement it (such as ReentrantLock). In this tutorial, we will see a basic usage of Lock interface to solve printer queue problem. 1. Lock Interface A java.util.concurrent.locks.Lock is a thread synchronization …
The factory design pattern is one of the most used design patterns in the java. It is one of creational patterns and can be used to develop an object in demand of one or several classes. With this factory, we centralize the creation of objects. The centralization of creation logic …
Today, one of the most critical aspects of a concurrent application is shared data. When you create a thread that implements the Runnable interface and then starts various Thread objects using the same Runnable object. All the threads share the same attributes that are defined inside the runnable object. This …
1. UncaughtExceptionHandler Java applications have two kind of exceptions – checked exceptions and unchecked exceptions. Checked exceptions must be specified in the throws clause of a method or caught inside them. Unchecked exceptions don’t have to be specified or caught. When a checked exception is thrown inside the run() method …
One of the best additions in java 5 was Atomic operations supported in classes such as AtomicInteger, AtomicLong etc. These classes help you in minimizing the need of complex (un-necessary) multi-threading code for some basic operations such as increment or decrement a value which is shared among multiple threads. These …
Multi-threading is very popular topic among interviewers from long time. Though I personally feel that very few of us get real chance to work on a complex multi-threaded application (I got only one chance in last 7 years), still it helps in having the concepts handy to boost your confidence …
Defining thread safety is surprisingly tricky. A quick Google search turns up numerous “definitions” like these: Thread-safe code is code that will work even if many Threads are executing it simultaneously. A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe …
Learn the basics of CountDownLatch and how to use it in real-life applications, its methods and use it to control the flow of application.
In Java Concurrency, until we have a particular reason, it is always recommended to implement the Runnable interface to create new threads.
Learn the differences between sleep() and wait() methods in Java. Learn when o use which method and what effect they bring in Java concurrency.
In Java, a synchronized block of code can only be executed by one thread at a time. Also, java supports multiple threads to be executed concurrently. This may cause two or more threads to access the same fields or objects at same time. Synchronization is the process which keeps all …
Java ThreadPoolExecutor & BlockingQueue example to run a demo task with fixed size thread pool. Also, learn to use RejectedExecutionHandler.
Learn to create a deadlock in Java programmatically, with an example. Also, learn to detect deadlock and how to solve a deadlock situation.
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.