Friday, April 26, 2019

Main Thread in Java

When a Java program starts, one thread starts running immediately that thread is known as main thread in Java.

If you run the following program where you have not created any thread, still on printing the thread name you can see that the thread name is displayed as “main”. This is the main thread which started running automatically.

public class ThreadDemo {
    public static void main(String[] args) { 
     System.out.println("Thread name- " + Thread.currentThread().getName());
    }
}

Output

Thread name- main

Main thread - Java multi-threading

A thread spawned from the current thread inherits the following properties from the parent thread-

Since main thread is the first thread that is started so other threads are spawned from the context of the main thread and other threads will inherit above mentioned properties from main thread.

Also note that the JVM terminates only after all the non-daemon threads have finished execution. In your application if you have other non-daemon threads executing then main thread can terminate before those threads. So, main thread is the first to start in your application but it doesn’t have to be the last to finish.

Here is a Java example of main thread where three more threads are spawned. Since all the three threads are spawned from main thread so they should have same priority and should be non-daemon threads. Main thread object is passed to the spawned threads to keep checking that main thread is alive or not using isAlive() method.

class NumThread implements Runnable{
    Thread mainThread;
    public NumThread(Thread thread) {
        this.mainThread = thread;
    }
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+ " Priority " 
         + Thread.currentThread().getPriority());
        System.out.println("Is Daemon Thread " + Thread.currentThread().isDaemon());
        for (int i = 0; i < 5; i++) {             
            System.out.println(Thread.currentThread().getName() + " : " + i);
        } 
        System.out.println("Thread name " + mainThread.getName());
        System.out.println("Main Thread Status " + mainThread.isAlive());     
    }
}

public class MainThreadDemo {

    public static void main(String[] args) {
        System.out.println("Thread name- " + Thread.currentThread().getName());
        System.out.println("Thread Status " + Thread.currentThread().isAlive());
        System.out.println("Thread Priority " + Thread.currentThread().getPriority());
        System.out.println("Is Daemon Thread " + Thread.currentThread().isDaemon());
         // 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();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        t2.start(); 
        t3.start();
        System.out.println("Thread Status " + Thread.currentThread().isAlive());
    }
}

Output

Thread name- main
Thread Status true
Thread Priority 5
Is Daemon Thread false
Thread-1 Priority 5
Is Daemon Thread false
Thread-1 : 0
Thread-1 : 1
Thread-1 : 2
Thread-1 : 3
Thread-1 : 4
Thread name main
Main Thread Status true
Thread Status true
Thread-2 Priority 5
Is Daemon Thread false
Thread-2 : 0
Thread-2 : 1
Thread-2 : 2
Thread-3 Priority 5
Is Daemon Thread false
Thread-2 : 3
Thread-2 : 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

As you can see for all the threads, thread priority is 5 which is same as the priority of main thread. Also all the spawned threads are non-daemon threads. You can also see from the displayed messages that main thread died while thread-2 and thread-3 were still executing.

That's all for this topic Main Thread in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Creating Thread in Java
  2. Thread States in Java Multi-Threading
  3. Race Condition in Java Multi-Threading
  4. Why wait(), notify() And notifyAll() Must be Called Inside a Synchronized Method or Block
  5. Volatile in Java

You may also like-

  1. Difference Between Encapsulation And Abstraction in Java
  2. static Reference to The Non-static Method or Field Error
  3. Difference Between throw And throws in Java Exception Handling
  4. CallableStatement Interface in Java-JDBC
  5. Reflection in Java
  6. Nested Class And Inner Class in Java
  7. Object Cloning in Java
  8. Just In Time Compiler (JIT) in Java

No comments:

Post a Comment