Java Thread and Multithreading Tutorial

Java Thread is a lightweight process that executes some task. Java provides multi-threading 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.

We can create Threads by either implementing Runnable interface of by extending Thread Class.

        Thread t = new Thread(new Runnable(){
            public void run() {

Above is a one liner statement to create new Thread, Here we are creating Runnable as Anonymous Class, read this post to learn about inner class, nested class and anonymous inner class.

In last few weeks, I have posted some useful articles on Threads, you can follow them in order to learn about Threads in Java.


  1. Java Thread Example

    This is the first post in the thread 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.
    It 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 does 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 methods in a simple Waiter-Notifier 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 and learn about the correct usage of synchronization and 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 and what are thread safety issues with them and how we can achieve thread safety in Singleton class.

  9. Java Daemon Thread

    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, learn in this post how we can create ThreadLocal variables in java program.

  11. Java Thread Dump

    Java Thread dump provides the current threads information for the program, this information is very helpful in analyzing performance issues with the application or 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 and 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 these 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.

This list will keep growing as more threads tutorials will be posted, so make sure to bookmark it for future use.

Please let us know if something is missing here that you want to learn about.

Leave a Reply



Current day month ye@r *