Provide Best Programming Tutorials

Main Thread In Java

The main thread in Java

Java provides built-in support for multithreaded programming. A multi-threaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.

Main Thread

When a Java program starts up, one thread begins running immediately. This is usually called the main thread of our program because it is the one that is executed when our program begins.

Properties :

  • It is the thread from which other “child” threads will be spawned.

  • Often, it must be the last thread to finish execution because it performs various shutdown actions

Flow diagram :

How to control Main thread

The main thread is created automatically when our program is started. To control it we must obtain a reference to it. This can be done by calling the method currentThread( ) which is present in Thread class. This method returns a reference to the thread on which it is called. The default priority of the Main thread is 5 and for all remaining user threads priority will be inherited from parent to child.

// Java program to control the Main Thread 
public class Test extends Thread { 
  public static void main(String[] args) { 
    // getting reference to Main thread 
    Thread t = Thread.currentThread(); 
    
    // getting name of Main thread 
    System.out.println("Current thread: " + t.getName()); 
    
    // changing the name of Main thread 
    t.setName("Geeks"); 
    System.out.println("After name change: " + t.getName()); 
    
    // getting priority of Main thread 
    System.out.println("Main thread priority: "+ t.getPriority()); 
    
    // setting priority of Main thread to MAX(10) 
    t.setPriority(MAX_PRIORITY); 
    
    System.out.println("Main thread new priority: "+ t.getPriority()); 
    
    
    for (int i = 0; i < 5; i++){ 
      System.out.println("Main thread"); 
    } 
    
    // Main thread creating a child thread 
    ChildThread ct = new ChildThread(); 
    
    // getting priority of child thread 
    // which will be inherited from Main thread 
    // as it is created by Main thread 
    System.out.println("Child thread priority: "+ ct.getPriority()); 
    
    // setting priority of Main thread to MIN(1) 
    ct.setPriority(MIN_PRIORITY); 
    
    System.out.println("Child thread new priority: "+ ct.getPriority()); 
    
    // starting child thread 
    ct.start(); 
  } 
} 

// Child Thread class 
class ChildThread extends Thread{ 
  @Override
  public void run(){ 
    for (int i = 0; i < 5; i++){ 
      System.out.println("Child thread"); 
    } 
  } 
} 

 

Output:

Current thread: main
After name change: Geeks
Main thread priority: 5
Main thread new priority: 10
Main thread
Main thread
Main thread
Main thread
Main thread
Child thread priority: 10
Child thread new priority: 1
Child thread
Child thread
Child thread
Child thread
Child thread

 

The relation between the main() method and main thread in Java

For each program, the Main thread is created by JVM(Java Virtual Machine). The “Main” thread first verifies the existence of the main() method, and then it initializes the class. Note that from JDK 6, main() method is mandatory in a standalone java application.

Leave a Reply

Close Menu