Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is java main method a thread? [duplicate]

I was wondering if Java main method was a thread or not. I noticed that if the main method returns, all threads are killed. Can you explain me why this happens? Thank you in advance.

like image 214
user3067088 Avatar asked May 04 '26 04:05

user3067088


2 Answers

Actually the main method runs on the Main thread ! The main threads invokes your main()

All the other threads of a java application are spawned from this thread !

like image 66
ItachiUchiha Avatar answered May 05 '26 17:05

ItachiUchiha


When you start your application the OS create a Thread for you which is the main thread (or UI thread in other contexts).

This thread just run your main method and stop. Anyway there are two types of threads.

  • User thread
  • Daemon threads

The main thread is a User thread. The JVM will be stopped when all user threads are finished.

Well, it's just a "fast response" i'm pretty use you can found better answer by experts here.

like image 23
Marco Acierno Avatar answered May 05 '26 16:05

Marco Acierno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!