×

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads. The execution of one thread is independent of another thread. Thus, when some error/ exception is generated in one thread, it does not hamper the process of another thread. All threads of the same process share the same memory space. Multithreading is the process of handling more than one thread simultaneously. The multithreading program in Java demonstrates the usage/ implementation of multithreading in the programming world.

Purpose of Multithreading

Multithreading ensures the maximum utilization of the CPU time when a program is executed. If a program is required to complete more than one task, which is independent of each other, multithreading is required.

Different States of a Thread in Java

The different states of a Thread are:

  • New: A thread whose execution has not started comes in this stage.
  • Runnable: After the start() method is called, the thread is in the queue for processing and ready to run, i.e., the thread scheduler has not selected the thread to move it to the running state. In other words, threads in this stage are waiting for the CPU for execution.
  • Running: The thread scheduler has moved the thread from runnable to this stage, which means the thread gets the CPU, and its execution is started. Note that the Runnable and Running stages form the active state of a thread.
  • Blocked/ Waiting: A thread that is still alive but is not eligible to enter the active state. A thread may enter this stage if a high-priority thread intervenes.
  • Dead/ Terminated: A thread that can no longer be used in a program. Usually, a thread enters this stage when the thread has completed its task or is forcefully terminated.

The following figure depicts the states of a thread.

Multithreading Program in Java

Creating a Thread in Java

There are two ways to create thread in Java.

1) By inheriting the Thread class

2) By implementing the Runnable Interface

Let’s discuss each of these given ways.

By inheriting the Thread class

Thread class implements the Runnable interface. In the Runnable interface, there is a method called run() that is like the service method of the thread. We need to override it in our thread class. See the following example.

FileName: UserThreadExample.Java

 // A class that inherits the class Thread
class MyThread extends Thread
{
   String m;
   int x;
    // constructor of the class
    public MyThread(String m)
    {
        this.m = m;
        x = 5;
    }
    // implemeting run() from
    // the Runnable interface
    public void run()
    {
        for(int i = 1; i <= x; i++)
        {
            System.out.println(m);
        }
    }
}
public class UserThreadExample
{
    // driver method
    public static void main(String argvs[])
    {
        MyThread t1 = new MyThread("Hello");
        MyThread t2 = new MyThread("Hi");
        MyThread t3 = new MyThread("Bye");
        // invoking the method start()
        t1.start();
        t2.start();
        t3.start();
    }
} 

Output:

 Hello
Bye
Hi
Hi
Hi
Hi
Hi
Bye
Bye
Bye
Bye
Hello
Hello
Hello
Hello 

Explanation: In the above code, we have created three threads. When the start() method is invoked, the thread enters in the active state. The start() method internally calls the run() method. A thread can never invoke the start() method more than once. If happens the same, it throws java.lang.IllegalThreadStateException is thrown. First, we have executed the threads t1, then t2, and finally t3. However, it does not guarantee that thread t1 is executed first. It is the duty of the thread scheduler to decide which thread will be executed first. We observe that four “Hello” are printing after “Bye”. If we execute our code again, it might happen we get different output. This is because context switching between the threads is not controlled by us.

Note: Every Java program has one thread called the main thread. The main thread is responsible for the execution of the main/ driver method. Thus, in the above example, there are 4 threads are executing. 

By Implementation the Runnable Interface

FileName: UserThreadExample1.Java

 // A class that implements the interface Runnable
public class UserThreadExample1 implements Runnable
{
   public void run()
   {
      System.out.println("Inside the method run ...");
   }
   public static void main(String argvs[])
   {
      // Creating an object of the class UserThreadExample1
      UserThreadExample1 usrObj = new UserThreadExample1();
      // Creating a thread t1 using the object usrObj
      Thread t1 =new Thread(usrObj);
      // starting the thread by invoking the start() method.
      t1.start();
   }
} 

Output:

Inside the method run ...

Explanation: The Runnable interface contains only a method called run(). The object (usrObj) is of the class UserThreadExample1. The class UserThreadExample1 implements the interface Runnable. It does not contain the method start(). The start() method is present in the class Thread. Therefore, we have created a thread t1 using the object usrObj. Finally, using the thread t1, the method start() is invoked to being the execution of the thread.

Priorities of a Thread

The scheduling of a thread is determined with the help of its priorities by an operating system. The range of priorities varies between 1 – 10. The MIN_PRIORITY takes the constant value 1, and MAX_PRIORITY takes the constant value 10. NORM_PRIORITY, which takes the constant value 5, is the default priority of a thread in Java. A thread with a high priority value is more important as compared to a thread with a low priority value. Therefore, the processor time of the high priority thread is allocated first, whereas the allocation of processor time is done later for the low priority thread. Note that high/ low priority does not guarantee the order of execution of threads. The order of execution of threads is very much dependent on the platform.


Related Topics

Java StringBuffer vs StringBuilder

Java StringBuffer vs StringBuilder StringBuffer The StringBuffer class is used to create mutable string. StringBuffer shows writable and growable character sequences. Java StringBuffer program FileName: StringBufferExample.java // A basic program that demonstrates the working...

2 minutes read.

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.

How to check the Java version in cmd

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated...

5 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Ordinal Number in Java

What is the Ordinal Number in Java? An object's or person's position or rank can be expressed mathematically using an ordinal number. Depending on the criteria used to establish the positions,...

3 minutes read.

How to use Lambda Expression in Java?

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single method interface using an...

4 minutes read.

Convert list to array Java

One of the popular collection interfaces for storing an ordered collection is the List. The List interface may contain repeating groups and preserves the insertion order of entries. This article will...

4 minutes read.

Collection Programs in Java

Collection Programs in Java Collection in Java provides a way to manipulate or store a group of objects. Each object in a collection is called element. Collection programs in Java mainly...

4 minutes read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.

Java Math random() Method

The random() method of Math class returns a double value with a positive sign, less than 1 and greater than or equal to 0.0. This method is properly synchronized with...

1 minute read.

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

Java HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

Access Modifier in Java

The access modifiers in java are used to change the accessibility and scope of a method, constructor, class, and fields. If you are aware of C++ language, when we declare any member...

2 minutes read.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

Compile time vs Runtime in java

Introduction: This article will discuss compile time vs. runtime in java. Compile time and runtime are two programming terms utilized in software improvement. Compile time is when the source code is...

3 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.