Print Friendly, PDF & Email

A thread represents the separate path of execution of a group of statements. In a java program, if we write a group of a statement, then these statements are executed by the Java Virtual Machine (JVM) one by one. This execution calls threads because the Java virtual machine uses a thread.

Note: A thread is a lightweight process because it utilizes the minimum resources of the system. This means they take less memory and fewer processes (minimum execution time).

In every program, at least one thread is executed that is the main thread. It is always created and executed in the program. In a single program, there may be many threads. Each of them is executed one by one in the memory by the Java virtual machine.

Task

A task means doing a calculation. A task is a small part of a program that is given to an interpreter to perform some calculation. A thread represents an execution of statements, the way the statements are executed as of two types:

Single-tasking 

In a single-tasking environment, only one task is given to the processor at the time of execution. In this tasking, only one separate memory is provided by the processor in which the task is performed.

Single tasking task execution

Multitasking

The second tasking is multitasking. Multitasking means executing multiple jobs at the same time. To use the processor time in an optimum way, we can give it several jobs at a time. This process is called multitasking.

Multithreaded Server is used for serving multiple clients concurrently

Note: Executing only one job at a time is called single-tasking while executing several jobs at a time is called multitasking. In single-tasking, the processor time is wasted but in multitasking, we can utilize the processor time as well as available resources in an optimal way.

Multitasking is the process of executing several jobs or multiple jobs at the same time. Multitasking can be categorized as:

  • Processed based multitasking (multiprocessing): Each process has its own memory address in the memory. In other words, we can say that every process has a separate path.
  • Thread-based multitasking (Multi-threading): In multithreading, a number of threads are executed at the same time. The communication between threads is very low. In multithreading, every thread shares the same address space or memory.

A visualization of how multithreading works

Every Java Program Uses Threads

Threading is an important concept in which we can execute multiple threads at a time with the help of the Java Virtual Machine.

If we write any Java program that has at least one thread, then that is the main thread. When we try to execute the program, Java Virtual Machine creates the main thread and calls the program’s main in which the main thread is available. Java Virtual Machine also creates other threads but it is invisible to the user because they are not so important for them.

Why Use Threads?

Threads are used in every Java program. This is true because of a number of reasons. Some of them are:

  • Make the User interface(UI) more responsive, which means more dynamic.
  • The main advantage of using threading concepts is multiprocessing. We can execute multiple jobs at the same time with the help of the processor.
  • It is more valuable in those places where the execution of the program performs in an Asynchronous way.

The process of multithreading

Creating Threads

We can create a thread in the program and execute the threads one by one. There are steps for creating a thread which are shown below:

Steps for Creating a Thread

  • Create a class that extends a thread class or implements runnable to the interface. (both are found in Java.lang package).
  • Write or provide the body of the Run () method.
  • Create an object of that class that contains the run () method.
  • Create a thread and attach it to the object of a class containing a Run () method.
  • Run the thread (for this purpose) use the Start () method of the Thread class.
  • Example to show (how to run and create a thread).

Thread t= new Thread();     //here Thread is the class and t is the object of thread class.

or

Thread t=new (object,”thread_name”);   //here we pass object and thread name.

Program to show the creation of a thread

public class sample

{

public static void main(String[]args)

{

System.out.println(“current thread”);

Thread t= Thread.currentThread();

System.out.println(“current Thread=”+t);

System.out.println(“name of the current Thread=”+t.getName());

}

}

Output

current Thread

current Thread=Thread(main,5,main)

name of the current Thread main.

Note: By default Run () method is recognized and executed by a thread.

Explanation of the program

In the above program, currentThread() is a static method in Thread class, so we call it as Thread.currentThread(). After this method, it gives an object t of Thread class. When we display object t, the following is shown:

currentThread=Thread[main,5,main]

Here Thread indicates Thread ()method, First main indicates the name of the thread running on the current code.

The number 5 represents the priority of the current threads (IE 1 to 10).

The next main indicates the thread group name to which the thread belongs. A thread group represents a group of the thread in a single unit.

The life cycle of a Thread 

The life cycle of a Thread

A thread is creating using the new Thread() statement and executes by the start() method. The thread inter runnable() states and when sleep(), suspend(), wait() method are used, or when the thread block on input/output, it then goes into a not runnable state(). From not runnable states the thread comes back to the runnable states and continues running the statements.

The thread dies when it comes out from the run() method. These state transitions of a thread are called a life cycle of a thread.

The Yield() method may pause a thread briefly, but the thread will be still in a runnable () state only.

The life cycle of a Thread from start to finish

Program to show how the start() method works

Here is a simple example that shows the execution flow of the program with the start()method.

class sample extends Thread

{

public void run()

{

int i;

for(i=1;i<=5;i++)

Sustem.out.println(i);

}

public class main class

{

public static void main(String[]args)

{

sample ob=new sample();

Thread t1=new Thread(ob);

Thread t2=new Thread(ob);

t.start();     //here the start() method call(execution start from from start method).

}

}

}

Thread Synchronization

When two or more threads need access to shared resources, they need some way to ensure that the resources will be used by only one thread at a time. The process by which we achieve this is called thread synchronization.

Thread synchronization imagined as a busy crossroad

Final Thoughts

In this article, I have provided simple and easy content. Any learner can easily read and understand the concepts of threading, Multi-threading, multitasking, etc. I also defined how we can understand the life cycle of the thread in simple words with the help of the images. I hope this article is much more beneficial for the learner.

LEAVE A REPLY

Please enter your comment!
Please enter your name here