Multithreading in Java
Multithreading in Java is a very important topic. I have written a lot about Threads in Java. Java Thread is a lightweight process that executes some task. Java provides multithreading support with the Thread class and an application can create multiple threads executing concurrently.
There are two types of threads in an application Ė user thread and daemon thread. When we start an application, main is the first user thread created and we can create multiple user threads as well as daemon threads. When all the user threads are executed, JVM terminates the program.
We can set different priorities to different Threads but it doesnít guarantee that higher priority thread will execute first than lower priority thread. Thread scheduler is the part of Operating System implementation and when a Thread is started, itís execution is controlled by Thread Scheduler and JVM doesnít have any control on itís execution.
Threads in Java
With Java 8 lambda expressions, we can create Thread in java like below too because Runnable is a functional interface.
1. Java Thread Example
This is the first post in the multithreading in java tutorial series. Read this to learn about Process and Thread. What is the difference between Thread and Process. Benefits of using Threads and how we can create Threads using Runnable interface and Thread class. This post also compares Runnable interface with Thread class.
2. Java Thread Sleep
Java Thread sleep is used to pause the execution of current thread. We will use Thread sleep extensively in future posts, so itís good to know how it works and is it accurate or not?
3. Java Thread Join
Sometimes we need to wait for other threads to finish itís execution before we can proceed. We can achieve this using Thread join, learn how it works and when we should use it.
4. Java Thread States
Understanding different states of thread is important. Learn how thread changes itís state and how thread scheduler changes thread state.
5. Java Thread wait, notify and notifyAll
Java Object class contains three methods using which threads can communicate about the lock status of a resource. Learn with example usage of these Object class methods in a simple Wait-Notify implementation.
6. Java Thread Safety and Java Synchronization
We know that Threads share Object resources, it can lead to data corruption because these operations are not atomic. Learn how we can achieve thread safety in java using different methods. Read this post to learn about the correct usage of synchronization, synchronized methods and synchronized blocks.
There are various examples of synchronized usage and the post explains what are the issues with them.
7. Java Exception in thread main
JVM creates first thread using main method. This post explains about some common exceptions we see in daily life and what is the root cause of them and how to fix them.
8. Thread Safety in Singleton Class
In this article, you will learn basic concepts of creating Singleton class. What are thread safety issues with different implementation. How we can achieve thread safety in Singleton class.
9. Daemon Thread in Java
A simple article explaining about daemon threads and how we can create daemon threads in java.
10. Java Thread Local
We know that threads share Objectís variables but what if we want to have thread-local variables created at class level. Java provides ThreadLocal utility class to create thread-local variables. Read more to learn about how we can create ThreadLocal variables in java program.
11. Java Thread Dump
Java Thread dump provides the current threads information for the program. Java Thread dump provides useful information to analyze performance issues with the application. You can use thread dump to find and fix deadlock situations. This post explains different methods that can be used to generate thread dump in java.
12. How to Analyze Deadlock and avoid it in Java
Deadlock is a situation where multiple threads are waiting for each other to release resources causing cyclic dependency. This article discusses about the situation in which we can get deadlock in a java program. How we can use Thread dump to find the deadlock and best practices to avoid deadlock in java program.
13. Java Timer Thread
This post explains how we can use Java Timer and TimerTask classes to create jobs to run at scheduled interval, an example program showing itís usage and how we can cancel the timer.
14. Java Producer Consumer Problem
Before Java 5, producer-consumer problem can be solved using wait() and notify() methods but introduction of BlockingQueue has made it very easy. Learn how we can use BlockingQueue to solve producer consumer problem in java.
15. Java Thread Pool
Java Thread Pool is a collection of worker threads waiting to process jobs. Java 5 introduction of Executor framework has made it very easy to create thread pool in java using Executors and ThreadPoolExecutor classes. Learn how to use them to create thread pool in java.
16. Java Callable Future
Sometimes we wish Thread could return values that we can use. Java 5 Callable can be used in that case that is similar as Runnable interface. We can use Executor framework to execute Callable tasks.
17. Java FutureTask Example
FutureTask class is the base concrete class that implements Future interface. We use it with Callable implementation and Executors for asynchronous processing. FutureTask provide implementation methods to check the state of the task and return the value to the calling program once itís processing is finished. It comes handy when you want to override some of the implementation methods of the Future interface.
by: siyaram ray