多线程父线程和子线程在Java中的执行

huangapple go评论77阅读模式
英文:

Multithreading Parent and Child thread execution in Java

问题

这是关于Java多线程的初学者问题。

据我理解,当创建多个(用户)线程来运行程序或应用程序时,不存在父线程和子线程的概念。它们都是独立的用户线程。

因此,如果主线程完成执行,那么另一个线程(Thread2)仍将继续执行,因为在Thread2的执行线程完成之前,JVM不会将其终止(https://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html,https://stackoverflow.com/a/9651919/6700081)。

那么,在下面的代码中,当主线程退出时,为什么我看不到来自.log()的日志被Thread2打印出来:

@Test
public void parentMainThreadAndChildThreadTest_WithSpringWebFlux() throws InterruptedException {
    Flux<Long> infiniteFlux = Flux.interval(Duration.ofMillis(100));
    infiniteFlux.subscribe((element) -> System.out.println("Value is:::" + element));
    Thread.sleep(3000); //主线程休眠3秒
}

我发现,如果我通过使主线程休眠来延长主线程的生命周期,那么我可以看到系统输出的语句。但是即使Thread2仍然异步运行,当主线程完成时为什么它们不会被显示?

在这种情况下,测试方法是由主线程的执行线程执行的,那么Thread2在主线程完成后会发生什么?

英文:

This is a beginner question regarding multithreading in Java.

As per my understanding when multiple (user)threads are created to run the program or application, then there is no concept of Parent and Child threads. They are both independent user threads.

So, if the main thread finishes execution then another thread(Thread2) will still continue its execution because it will not be killed by the JVM until the Thread2's thread of execution is completed (https://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html, https://stackoverflow.com/a/9651919/6700081)

Then why am I not seeing the logs from the .log() being printed by Thread2 when the Main thread exits in the below code:

    @Test
    public void parentMainThreadAndChildThreadTest_WithSpringWebFlux() throws InterruptedException {
        Flux&lt;Long&gt; infiniteFlux = Flux.interval(Duration.ofMillis(100));
        infiniteFlux.subscribe((element) -&gt; System.out.println(&quot;Value is:::&quot; +element));
        Thread.sleep(3000); //Main thread sleeps for 3 seconds
    }

I see that if I increase the main thread's life by putting it to sleep then I can see the system out statements. But why aren't they displayed when the main thread is finished even though the Thread2 is still running asynchronously?

The test method is executed by the Main thread's thread of execution so what happens to the Thread2 after main thread finishes in this case?

答案1

得分: 1

如果主线程执行结束,那么另一个线程(Thread2)仍将继续执行。

这只对普通线程成立。线程池中的线程通常被配置为守护线程,当所有普通线程结束时,它们会被强制停止。

在您的情况下,

(element) -> System.out.println("Value is:::" + element)

在从反应堆的线程池中获取的守护线程上执行。

英文:

if the main thread finishes execution then another thread(Thread2) will still continue its execution

this is true only for normal threads. Threads for thread pools usually are configured as daemon threads, which are forced to stop when all normal threads are finished.

In your case,

(element) -&gt; System.out.println(&quot;Value is:::&quot; +element)

is executed on a daemon thread taken from a reactor's thread pool.

huangapple
  • 本文由 发表于 2020年9月15日 00:30:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63888365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定