Multithreading in Java

Multithreading in Java

Multithreading in java is a process that allows executions of multiple threads in java. It helps in the maximum utilization of the CPU. We call each part of the program as a thread so multithreading helps in the executions of multiple parts of a program at a single time. Multithreading helps to create small multiple tasks in the application where we can subdivide the operations into single threads and perform the tasks.

Read Top Core Java interview questions

The cycle of the thread works in the following manner:

New thread() ->New -> Runnable -> Running -> Waiting -> Dead

New thread() ->New -> Dead

Description is as follows:

New: When a new state of the cycle is formed, the new life of the thread begins.

Runnable: we put a thread in the category of runnable when a new thread starts to perform action.

Waiting: when a thread is already performing a task, the other thread is kept on hold or wait. Once the previous thread finishes its task, the new thread comes into play.

Dead: we call a thread dead or in terminated state when it completes its task.

More Recommended Post

Types of Multithreading

There are mainly two types of threads in Java which can perform multithreading in the program. They are user thread and daemon thread.

  • User thread: When we start an application, the first thread which is created is called the user thread. A program can never terminate unless the user thread present in the main() terminates.
  • Daemon thread: This type of thread in the application provides services for user threads. It has no life of its own and it is completely dependable on user threads for survival. Hence, it is not a high priority thread as compared to user thread. They are mostly used for extra background tasks in the application. The methods in this thread are void setDaemon() which marks the current thread as a daemon thread.

Another method is isDaemon() which can be used by the user to check if the thread is Daemon or not.

We can create threads for multithreading using 2 criteria, they are:

  • Extending the Thread class: We can create this extension in java.lang.Thread class. There’s a run method in the tread class that gets overridden during this extension process. We create a new object for a class and start() method is used to start the execution of that thread.
  • By implementing a run-able environment: when we extend the runnable class, it can extend other base classes. It doesn’t support inbuilt functions such as interrupt(), yield() etc.

The program will simply terminate the daemon thread if the user thread is no longer in use. This is because the sole purpose of a daemon thread is to provide support for the tasks of user thread. Hence, JVM saves the space by demolishing the daemon thread when there is no user thread.

Thread Class Methods

  • getName(): this method helps in getting the name of the thread.
  • getPriority(): This will get the priority of the thread.
  • join(): this method will help a thread to end its execution or simply terminate.
  • run(): a thread starts its process using run() method.
  • sleep(): the thread terminates its execution completely using sleep().
  • isAlive(): this method is used to check if the thread is still in the execution state or if it is still running.

Below is a program to understand how multithreading in Java works:

package Test;
 public class DeveloperHelps implements Runnable
{
       public static void main(String[] args) {
        Thread T1 = new Thread("Thread 1 works");
        Thread T2 = new Thread("Thread 2 works");
        T1.start();
        T2.start();
        System.out.println("Thread names are following:");
        System.out.println(T1.getName());
        System.out.println(T2.getName());
    }
    @Override
    public void run() {
    }
}

The output of the above program will be:

The names of the threads are:
Thread 1 works
Thread 2 works

We have some advantages of multithreading:

  • While performing multithreading, users cannot be blocked. Threads are independent and as a result, we can perform multiple operations during one time.
  • If one thread stops or meets an exception, the other threads do not get affected. They can keep performing their respective tasks.
  • Multithreading helps in promoting better CPU utilization.
  • A user can design better and responsive programs due to the multithreading process in Java.
  • The process of multithreading helps in the fair distribution of CPU resources.

Leave a comment

Your email address will not be published. Required fields are marked *