When a Java application starts, execution typically begins on an initial thread commonly called the main thread. This thread executes the application's launchable main() method and can create additional threads during program execution.
- The main thread can be accessed using Thread.currentThread().
- Its default name in a typical Java application is main.
- Additional threads can be created from the main thread or from other threads.

Above diagram shows how the JVM creates and manages multiple threads during program execution. The main thread starts child threads, while the JVM also runs daemon threads such as the Garbage Collector in the background.
How the Main Thread Works
The main thread is created automatically when a Java program starts. Developers do not need to create it explicitly. The currently executing thread can be obtained using:
Thread.currentThread();
This method returns a reference to the thread that is currently executing the code. For a normal Java application, calling it inside main() generally returns the main thread.
Accessing and Modifying the Main Thread
The following example demonstrates how to:
- Access the main thread.
- Read and change its name.
- Read and change its priority.
- Create a child thread.
- Observe priority inheritance.
import java.io.*;
import java.util.*;
// Class 1
// Main class extending thread class
public class Test extends Thread {
// Main driver method
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);
// Print and display the main thread 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
Thread ct = new Thread() {
// run() method of a thread
public void run()
{
for (int i = 0; i < 5; i++) {
System.out.println("Child thread");
}
}
};
// 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();
}
}
// Class 2
// Helper class extending Thread class
// Child Thread class
class ChildThread extends Thread {
@Override public void run()
{
for (int i = 0; i < 5; i++) {
// Print statement whenever child thread is
// called
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
Explanation
- Thread.currentThread() returns the thread currently executing the code.
- getName() returns the thread name.
- setName() changes the thread name.
- getPriority() returns the current thread priority.
- setPriority() changes the thread priority.
- A newly created thread normally inherits the priority of the thread that creates it.
- start() begins execution of the child thread and causes its run() method to execute on a separate thread.
Relationship Between main() Method and Main Thread
- The main() method serves as the entry point for applications launched using the standard Java launcher. The selected initial class is loaded and initialized before its launchable main() method is invoked.
- The traditional entry point is public static void main(String[] args).
- Since Java 25, the launch protocol also supports certain instance main() methods and main() methods without parameters, so the traditional declaration is no longer the only supported form.
Deadlock Using Main Thread
Even a single main thread can cause deadlock if it waits for itself.
public class GFG {
public static void main(String[] args) {
// Try block to check for exceptions
try {
// Print statement
System.out.println("Entering into Deadlock");
// Joining the current thread
Thread.currentThread().join();
// This statement will never execute
System.out.println("This statement will never execute");
}
// Catch block to handle the exceptions
catch (InterruptedException e) {
// Display the exception along with line number
// using printStackTrace() method
e.printStackTrace();
}
}
}
Output:

Explanation:
- Thread.currentThread().join() tells the main thread to wait for itself to die.
- The thread waits indefinitely, causing a deadlock.
- Any code after join() will never execute.
Important Methods Used with the Main Thread
| Method | Description |
|---|---|
| Thread.currentThread() | Returns the currently executing thread |
| getName() | Returns the name of the thread |
| setName() | Changes the thread name |
| getPriority() | Returns the thread priority |
| setPriority() | Changes the thread priority |
| start() | Starts a new thread |
| join() | Waits for another thread to terminate |
| isAlive() | Checks whether a thread is still running |