How can thread run after main method closes?

后端 未结 6 1846
借酒劲吻你
借酒劲吻你 2021-02-13 22:41

Here are my two classes:

public class Firstclass {
    public static void main(String args[]) throws InterruptedException {
        System.out.println(\"Main sta         


        
6条回答
  •  孤独总比滥情好
    2021-02-13 23:27

    Your first question,

    When main method is called by default one main thread is created. And main thread is a non-dameon thread. When threads created by the main method it inherits it's parant's property. That means they are all non-daemon threads. As you know JVM waits until all non-daemon threads to complete. So it will executes even after the main thread completes.

    See here : daemon vs non-daemon Java Stack for each thread creation

    and your second question: join method joins the currently running thread at the end of the thread which is calling the join method. That means the current thread will be stopped and it start after the thread referenced by the join method.

    Synchronization stops two threads executing the same code at the same time.

提交回复
热议问题