简体   繁体   English

创建了多少个线程?

[英]How many threads being created?

In one of the interview i came across query on threads.Question is in the below code snippet, how many threads are being created? 在其中一个访谈中我遇到了关于threads.Question的问题,在下面的代码片段中, 创建了多少个线程? I know it's seems to be very basic query. 我知道这似乎是非常基本的查询。 But i couldn't find answer with proof. 但我无法找到证据答案。

Could someone provide me answer with the concept(any links)? 有人能给我答案的概念(任何链接)? Thanks in anticipation. 谢谢你的期待。

class MainApp {
    public static void main(String[] args) {
        System.out.println("Welcome to Java");
    }
}

Since your simple program does not create threads explicitly only one application thread is created here. 由于您的简单程序没有显式创建线程,因此这里只创建一个应用程序线程。 This is the "correct" short answer at the interview. 这是面试中的“正确”简答。

However you can continue and say that JVM creates other threads it needs for itself. 但是,您可以继续并说JVM创建自己需要的其他线程。 For example garbage collector (GC) thread. 例如垃圾收集器(GC)线程。 Number of GC threads depends on configuration. GC线程数取决于配置。 By default 1. 默认1。

If the question is literally: 如果问题是字面意思:

In the below code snippet , how many threads are being created? 在下面的代码片段中 ,创建了多少个线程?

...then the answer is 0 . ...然后答案是0 The code snippet doesn't create any threads in and of itself. 代码片段本身不会创建任何线程。 Of course, a thread gets created for that code to run in, and since that code doesn't create any more, another sensible answer would be 1. But if you're talking about how many threads the VM is using, then that's going to (usually) be at least two (for GC), most of the time more (dispatcher, reference handler, finalizer etc.) but entirely dependent on configuration. 当然,为该代码运行创建了一个线程,并且由于该代码不再创建,另一个合理的答案是1.但是如果你在谈论VM正在使用多少个线程,那么那就是到(通常)至少两个(对于GC),大多数时候更多(调度程序,参考处理程序,终结器等),但完全依赖于配置。 Even then, it won't always be two, you can have a perfectly conforming JVM that doesn't do garbage collection at all (and indeed the early JavaCard VM's did exactly this.) 即使这样,它不会永远是两个,你可以有一个完美的贴合JVM不会在所有做垃圾收集(实际上早JavaCard的VM的正是这一点做到了。)

Of course, that assumes that the interviewer was asking about Java threads - if we go down to the OS level, then again we have a further level of unknowns as to exactly how the system is printing out the above string, if at all! 当然,这假设访问者询问Java线程 - 如果我们进入操作系统级别,那么我们还有一个未知的关于系统如何打印出上述字符串的更多未知数,如果有的话!

Given the above, my response would have been to clarify what exactly the interviewer meant by the question - because in and of itself, it's a bit ambiguous and not a great question at all in my opinion. 鉴于上述情况,我的回答应该是澄清访问者对问题的确切含义 - 因为就其本身而言,它有点含糊不清,而且在我看来根本不是一个很好的问题。 It reads like a trick question, but without enough information to answer it properly - whatever answer you gave, without further clarification, could be deemed incorrect. 它看起来像一个技巧问题,但没有足够的信息来正确回答 - 无论你给出什么答案,没有进一步澄清,都可能被视为不正确。 Though as NoobUnchained has posted already, mentioning you can use Thread.activeCount() to grab the number of VM threads in use wouldn't go against you. 虽然NoobUnchained已经发布了,但提到你可以使用Thread.activeCount()来获取正在使用的VM线程数量不会对你不利。

Of course, perhaps the purpose of the question was not so much for a technical answer but to see if you would clarify what exactly was meant by it at interview - not an unheard of tactic! 当然,也许这个问题的目的不是为了获得技术答案,而是为了看看你是否会澄清它在采访中究竟是什么意思 - 而不是闻所未闻的战术!

When a program is running, there are threads that we create that are running. 程序运行时,我们创建的线程正在运行。 In your case, it's a simple program, then there is a main thread which executed the main method . 在你的情况下,它是一个简单的程序,然后有一个执行main方法主线程 Also there might few daemon threads run by the JVM (ie maybe the garbage collector and more). 此外, JVM可能运行的守护程序线程很少(即可能是垃圾收集器等)。

The correct answer is "I don't know" I got these threads created: 正确的答案是“我不知道”我创建了这些线程:

 Signal Dispatcher
 main
 Finalizer
 Reference Handler
 Monitor Ctrl-Break

It can depend on the jvm and its configuration. 它可以依赖于jvm及其配置。

It all depends which thread you are trying to count. 这一切都取决于你想要计算的线程。 There are two types of threads being created by the JVM one is application level thread and the others are System level threads. JVM创建了两种类型的线程,一种是应用程序级线程,另一种是系统级线程。

So atleast one thread is created which is the main thread and many System threads such as for memory management (Garbage Collection) etc. 因此创建了至少一个线程,它是主线程和许多系统线程,例如内存管理(垃圾收集)等。

If you take your code for example only 1 application level thread is created which is the main thread. 例如,如果以代码为例,只创建一个应用程序级别的线程,这是主线程。 And the code itself doesn't create further threads. 代码本身不会创建更多的线程。

So a precise answer will be 1 application level thread (Main thread) and many System level threads 因此,精确的答案将是1个应用程序级线程(主线程)和许多系统级线程

Read below Reference copied from Java Documentation: 阅读以下从Java文档复制的参考:

http://docs.oracle.com/javase/tutorial/essential/concurrency/procthread.html http://docs.oracle.com/javase/tutorial/essential/concurrency/procthread.html

Multithreaded execution is an essential feature of the Java platform. 多线程执行是Java平台的基本特性。 Every application has at least one thread — or several, if you count "system" threads that do things like memory management and signal handling. 每个应用程序至少有一个线程 - 或几个,如果你计算“系统”线程,它们执行内存管理和信号处理等操作。 But from the application programmer's point of view, you start with just one thread, called the main thread. 但是从应用程序员的角度来看,你只从一个线程开始,称为主线程。 This thread has the ability to create additional threads, as we'll demonstrate in the next section. 该线程具有创建其他线程的能力,我们将在下一节中进行演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM