Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Multithreading Introduction

We will start the discussion of Multithreading first we must have to understand what Multitasking is?

Performance or executing more than one task at the same time is called as multitasking.

Note:-As of now, there is no machine or processor that can perform more than one task at the same time. However, the time duration required to complete the task is very short, making it challenging to analyze whether the task is being done simultaneously or at different times.

There are mainly 2 different type of Multitasking:

1. Process based multitasking
2. Thread based multitasking

Note:- While threads will share resources, which will be decided by the CPU scheduling algorithm and managed by the thread life cycle.
Each thread in Java will be managed by a special life cycle mechanism; the processing of all these will completely depend upon the scheduling algorithm.

Java Thread:

In Java we have 3 different API to manage the thread:

1. java.lang.Thread Class
2. java.lang.ThreadGroup Class
3. java.lang.Runnable Interface

Now, we will understand how Java threads internally work when we run a basic Java program. As we know, in Java, we can only use a .class file that contains the main method; otherwise, we get an error at the time of execution, which is MainMethodNotFoundError.

So, first, let's understand how the JVM internally uses a thread to locate the main method:

Whenever the JVM starts internally, it creates a ThreadGroup named 'main,' and under that ThreadGroup, one Thread is created with the name 'main'.
In Java, we use a Thread to perform a specific task. When we observe the main thread's task, we find the following points:

1.The main thread internally obtains the name of the main() method using the reflection mechanism.
2.Internally, the main thread creates a default object of type java.lang.Class.
3.It creates a string object to pass as a parameter to the main() method.
4.The main thread sets command-line arguments for the main() method if any are available.
5.The main thread calls the main() method, passing the string reference as a parameter. After completing the specified task, the thread moves to the dead state.
6.All child threads are created from the main thread itself.

            
class JTC{
	public static void main(String arg[]){
		System.out.println("------Main in class JTC-------");
		System.out.println("Current Thread :- "+Thread.currentThread().getName());
		System.out.println("Current Thread Group :- "+Thread.currentThread().getThreadGroup().getName());
	}
}
    ------Main in class JTC-------
    Current Thread :- main
    Current Thread Group :- main
            

In the above example, we retrieve the current thread name and the current thread group of the main method. We observe that the current working thread is named 'main,' and its thread group is also 'main'.

To obtain the current thread of the main method, we utilize the currentThread() method of the java.lang.Thread class. Similarly, to retrieve the current thread group, we employ the getThreadGroup() method of the java.lang.Thread class

public static native java.lang.Thread currentThread();

Member Type: - Static Method
Access Modifier: - public
Return Type: - java.lang.Thread
Method Name: - currentThread
Functionality: - Since this is a native method, its implementation is written in a programming language other than Java. In Java, every thread is represented as an object of the `java.lang.Thread` class. When we invoke the `currentThread()` method, it returns an object of the `java.lang.Thread` class that is associated with the current method, block, or constructor.

public final java.lang.ThreadGroup getThreadGroup();

Member Type: - Instance Method
Access Modifier: - public
Return Type: - java.lang.ThreadGroup
Method Name: - getThreadGroup
Functionality: - This method is declared as a final type, meaning we cannot alter its implementation in a subclass of the java.lang.Thread class. When we invoke this method with a Thread or an object of the java.lang.Thread class, it returns an object of the java.lang.ThreadGroup class associated with the current working thread group.

Note: In Java every thread is an object of java.lang.Thread class.

            
class JTC{
	public static void main(String[] args) {
		Thread th1 = new Thread();
		System.out.println(th1);
		String name = th1.getName();
		System.out.println(name);
		int p = th1.getPriority();
		System.out.println(p);
		th1.setName("Jtc-th1");
		System.out.println(th1.getName());
		th1.setPriority(9);
		System.out.println(th1.getPriority());
		Thread th2 = new Thread();
		System.out.println(th2);
		Thread th3 = new Thread("Jtc");
		System.out.println(th3);
	}
}
    Thread[Thread-0,5,main]
    Thread-0
    5
    Jtc-th1
    9
    Thread[Thread-1,5,main]
    Thread[Jtc,5,main]
            

In this example, we create two objects of the java.lang.Thread class, namely th1 and th2. We then print their names, priorities, and their Thread Group names.
Upon creating the first object of the java.lang.Thread class and printing its details, we obtain the output: Thread [Thread-0, 5, main]. Here, Thread-0 is the name of the thread, 5 is the priority, and main is the ThreadGroup.
Similarly, the output for the second thread is Thread [Thread-1, 5, main].
At this point, we observe the default naming pattern of the Thread Name, which is Thread-0, Thread-1, and so on. The default priority of the Thread is 5, and the default Thread Group is main.
However, as per requirements, we can set the thread name using the setName(String) method and adjust the Thread's priority using the setPriority() method.

Note:- we will discuss about the Thread Priority and Thread Group into the further tutorial.