Hi I wanted to know how and where the main method will create thread for executing the application. whether it is extends thread or runnable.
3 Answers
That is a nice question. When ever you execute your application JRE will create a thread for it. That thread will execute your main()
method. It will be finished when the program reaches to the end. Obviously it should be the last thread to be ended. It extends Thread
class and can be accessed by using like:
class ThreadTest {
public static void main(String [] args){
Thread mainThread = Thread.currentThread();
}
}
-
1_Obviously it should be the last thread to be ended._ This is wrong, actually [threads can continue to run even after `main` thread has ended](http://stackoverflow.com/questions/13904745) – ADTC Jan 07 '14 at 13:15
-
whether that main will extend thread class as u say. – user3168013 Jan 07 '14 at 13:30
the main thread is the first thread to execute in a program.it is created by jvm.

- 6,498
- 5
- 49
- 60
-
-
the main thread is called by jvm,when a program run the jvm check for main method and start execution,only child threads of main thread can be creaed using extends or runnable – Lijo Jan 07 '14 at 12:37
I wanted to know how and where the main method will create thread for executing the application.
It doesn't.
What actually happens is that something creates the main
thread, and the main
thread then calls the public static void main(String[])
method in the relevant class.
How this happens is implementation dependent. If you really want to understand the details, the complete OpenJDK codebases for Java 6, 7 & 8 (beta) are available for download.
whether it is extends thread or runnable.
It is not specified whether the main thread is an instance of Thread
or a subclass of Thread
. It cannot (just) be Runnable
, though a Runnable
could be used to call the main
method.
However, the main
method can find out what the main thread's actual class is as follows:
class Test {
public static void main(String [] args){
Thread t = Thread.currentThread();
System.out.println("Main thread class is " + t.getClass());
}
}

- 698,415
- 94
- 811
- 1,216