Java is one of the first programming language to provide built-in support for multi-threading. In fact when a Java program starts one thread starts running immediately that thread is known as main thread in Java.
If you ever tried to run a Java program with compilation errors you would have seen the mentioning of main thread. Here is a simple Java program that tries to call the non-existent getValue() method.
public class TestThread { public static void main(String[] args) { TestThread t = new TestThread(); t.getValue(); } }
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method getValue() is undefined for the type TestThread
As you can see in the error when the program is executed, main thread starts running and that has encountered a compilation problem.
Java Main Thread
Main thread in Java is important as your Java program will start running on this thread. Additional threads spawned in your program will inherit some of the properties from the main thread like thread priority, creating threads as non-daemon threads as main thread is a non-daemon thread.
By default name of the main thread is "main" and the thread priority of main thread is 5. This main thread belongs to a thread group called main.
The Java Virtual Machine continues to execute threads until all threads that are not daemon threads have died. If you have spawned other threads in your program that are non-daemon then main thread may terminate before those threads. Let’s see a Java example to clarify these statements.
Main thread Java example
In the program we’ll display the name of the thread in the main method and using isAlive() method it is also verified whether thread is still alive or terminated.
Three more threads are also spawned, in order to keep checking on the status of the main thread its reference is sent to the class that implements Runnable.
class NumThread implements Runnable{ Thread thread; public NumThread(Thread thread) { this.thread = thread; } @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " : " + i); } System.out.println("Thread name " + thread.getName()); System.out.println("Main Thread Status " + thread.isAlive()); } } public class ThreadPriority { public static void main(String[] args) { // Information about main thread System.out.println("Thread name- " + Thread.currentThread().getName()); System.out.println("Priority of " + Thread.currentThread().getName() + " thread is " + Thread.currentThread().getPriority()); System.out.println("Group " + Thread.currentThread().getName() + " thread belongs to- " + Thread.currentThread().getThreadGroup()); // Creating threads Thread t1 = new Thread(new NumThread(Thread.currentThread()), "Thread-1"); Thread t2 = new Thread(new NumThread(Thread.currentThread()), "Thread-2"); Thread t3 = new Thread(new NumThread(Thread.currentThread()), "Thread-3"); t1.start(); t2.start(); t3.start(); System.out.println("Thread name " + Thread.currentThread().getName()); System.out.println("Thread name " + Thread.currentThread().isAlive()); } }Output
Thread name- main Priority of main thread is 5 Group main thread belongs to- java.lang.ThreadGroup[name=main,maxpri=10] Thread name main Thread name true Thread-1 : 0 Thread-1 : 1 Thread-1 : 2 Thread-1 : 3 Thread-1 : 4 Thread name main Main Thread Status false Thread-3 : 0 Thread-3 : 1 Thread-3 : 2 Thread-3 : 3 Thread-3 : 4 Thread name main Main Thread Status false Thread-2 : 0 Thread-2 : 1 Thread-2 : 2 Thread-2 : 3 Thread-2 : 4 Thread name main Main Thread Status false
As you can see from the output priority of the main thread is 5 and the thread group is also main. main You can also verify that the main thread is terminated while other threads are still executing.
That's all for the topic Main Thread in Java. If something is missing or you have something to share about the topic please write a comment.
You may also like
- How to Create And Start Thread in Java
- Daemon Thread in Java
- Sleep Method in Java Multi-Threading
- final in Java With Examples
- Java throws Clause With Examples
- Built-in Functional Interfaces in Java
- Java Program to Get Files Having a Specific Extension
- First React App – Hello World React Example
- Spring IoC Container Types – ApplicationContext and BeanFactory
- HDFS Replica Placement Policy
No comments:
Post a Comment