i know main() is a thread, only because of cmd says. But i want to know how it can be a thread if we not extends Thread class or implements Runnable interface?
Asked
Active
Viewed 387 times
1
-
6Compiler magic. – litelite Aug 02 '17 at 16:54
-
2Bascially the JVM launches a thread for you before your application starts. And that thread is likely created by the OS when the JVM starts up: so OS magic. – markspace Aug 02 '17 at 16:57
-
JVM launvhes thread called The main thread is the thread that starts your program, or simply which runs your public static void main(String... args) method. – Fady Saad Aug 02 '17 at 16:58
-
Check this nice answer: [SO answer to this question](https://stackoverflow.com/a/17433094/912829) [When the main thread stops](https://stackoverflow.com/questions/7416018/when-does-the-main-thread-stop-in-java) – ACV Aug 02 '17 at 17:06
-
1So, I think the key thing here is to familiarize yourself with the concept of a thread; it is a **unit of execution**. The program's main function starts in a thread that is implicitly created by the JVM because it must be executed in **a** thread. – Evan Teran Aug 02 '17 at 17:34
2 Answers
4
When the JVM starts, it creates a thread named main. This thread representes the main thread entry and the whole application will execute on it unless you start new threads from this one.
The main thread will then lookup for the static void main(String[])
within your entry class to execute it being your program entry point.
You should not then confuse the main thread and the #main(String[])
method as they are totally separate things.

tmarwen
- 15,750
- 5
- 43
- 62
0
All processes on most operating systems run as threads, so it is impossible for a Java main method not to be a thread. Although it doesn't extend or implement the Thread class, it is still running as a thread under the visible code.

Daniel Williams
- 635
- 7
- 14