Multithreading in Java

Multithreading in java is the most important topic in java. In this post, we will discuss the Multithreading in java example, and we will try to covert real time example of multithreading in java.  

Here is the table content of the article will we will cover this topic.
1. What is multithreading in Java?
2. Important points about multithreading?
3. How does thread work?
4. Advantages of multithreading in Java?

5. Thread in java
6. Creation of thread in java
7. Memory representation of thread creation in java
8. Start() and run() method?

9. Thread life cycle in java
10. Thread sleep() method?

11. The main thread in java
12. Thread scheduler in java
13. Java Thread Sleep
14. join() method in java
15. yield() method in java

16. wait() method in Java
17. notify() method in Java
18. Difference between wait and sleep in java
19. Thread priority in java
20. Thread class in java
21. Naming a thread in java

22. Daemon thread in java

Before understanding the concept of multithreading in java, we need to understand the concept of thread and process. After that, we will see the multithreading program in java.

Process: The execution of program is referred as process. In other hand we can say when we are executing a program it means a process is in running. A program can be divided into small processes(sub process) also.

Thread: A thread is the smallest unit of process or a lightweight sub-process. A program can have any number of sub-processes and each sub-process runs parallelly by a thread. A thread behaves like a separate CPU executing the process, but CPU shares the time between threads. Each program has at least one thread. If we create a simple program to print “Hello world”, it means at least one thread will exist in the program. We will discuss it later in detail.

public class MainClass 
{
    public static void main (String [] args) 
    {
    	System.out.println("Hello world");
    	System.out.println("Thread name : "+Thread.currentThread().getName());
    }
}

Output: Hello world
Thread name : main

What is multithreading in Java?

When a process is executing by multiple threads simultaneously is known as multithreading in java. As we know a process can have a number of subprocesses and each subprocess can be executed in parallel by threads. An application can have any number of processes. Each process can be assigned to a single thread or multiple threads.

OR

Multithreading in java is a process, in which two or more threads can run the process and do the maximum utilization of CPU. It is also known as Concurrency in Java. Each thread can run in parallel with each other.
Java supports multithreading, it means we can develop a program that can have to or more part(sub-processes) that can run concurrently and each part(subprocess) can have a different task. By the use of multithreading, we can do maximum optimization of CPU.

Multithreading in Java

Important points about multithreading in Java

1. Multithreading is used to utilize the maximum time of CPU. In multithreading, we can execute the two or more parts of a program simultaneously. Each part of the program can run separately by single or multiple threads.

2. A Thread is a lightweight sub-process, that can separately from other threads. The multiple threads can share the common memory space. The CPU divides the time for all thread and switch from one thread to another thread for a while.

3. A thread runs independently because it has a separate path of execution.

How does thread work?

As we already discussed thread is the lightweight and smallest part of the process. A process can have one or more threads. Each process has at least one thread. To complete a process, the subprocesses(thread) can work independently and provide better performance. Multithreading means you can have multiple threads of execution inside the same program. If you have a number of threads that are executing the same program, It behaves like have multiple CPUs execute the same program.

Multithreading in Java

Advantages of multithreading in Java

Before moving further, we should learn what is the advatanges of multithreading and why it is so popular.

Maximum utilization of a single CPU.

As we know CPU is much faster than other resources like network, I/O operations. Multithreading is to be able to better utilization of the CPU. Let’s see the multithreading in java example.

Suppose CPU has only a single process that handled by a single thread. The thread is waiting for the response from the I/O operation meanwhile the CPU is in the ideal state because it has not any other thread to process.
In multithreading, multiple threads can work parallelly so if one thread is waiting for any resource then another thread could use the CPU.

Give fast response

By use of multithreading, we can provide a fast response to the users that makes a better experience. Suppose we are using multithreading in our application. The user submitting the form by click on a button, by use of one thread we will submit the form on the network and wait for response meanwhile another thread can show loading icon on the screen that gives the user a better experience.

Better user experience

Suppose we are receiving requests from many clients and processing them. If we are using one thread for processing then we can’t do response faster because there may be a requests that take a long time to process. Then all other clients would wait until that one request has finished. This type of situation can get frustrate the clients. But if we will use the multiple threads then we can handle multiple requests at the same time.

6 thoughts on “Multithreading in Java”

  1. really very easy to understand and helpful..article and a best website..for us..plz updates all new features in java…time by time @jay…Thank you

    Reply

Leave a Comment