[英]How many threads is it advisable to have running at the same time in Java?
I am new to multithreading in Java, after looking at Java virtual machine - maximum number of threads it would appear there isn't a limit to how many threads a Java/Android app can run. 在看过Java虚拟机之后,我对Java中的多线程是陌生的-它出现的最大线程数对 Java / Android应用程序可以运行的线程数没有限制。 However, is there an advisable limit?
但是,是否有一个适当的限制? What I mean by this is, is there a number of threads where if you run past this number then it is unwise because you are unable to determine what thread does what at what time?
我的意思是,是否有多个线程在其中运行,如果您超过该数目,那么这是不明智的,因为您无法确定什么线程在什么时间做什么? I hope my question makes sense.
我希望我的问题有道理。
There are some advisable limits, however they don't really have anything to do with keeping track of them. 有一些建议的限制,但是与跟踪它们并没有任何关系。
TL;DR TL; DR
Depends on OS and Hardware: on servers creating thousands of threads is fine, on desktop machines you should limit yourself to 50-200 and choose carefully what you do with them. 取决于操作系统和硬件:在创建数千个线程的服务器上很好,在台式机上,您应将自己限制为50-200,并仔细选择对它们的操作。
Note: Androids default and suggested "UI multithread helper" - the AsyncTask is not actually a thread. 注意: Androids默认并建议使用“ UI multithread helper”-AsyncTask实际上不是线程。 It's a task invoked from a ThreadPool, and as such there is no limit or penalty to using it.
这是从ThreadPool调用的任务,因此使用它没有限制或惩罚。 It has an upper limit on the number of threads it spawns and reuses them rather than creating new ones.
它对产生和重用它们而不是创建新线程的线程数量有一个上限。 Most Android apps should use it instead of spawning their own threads.
大多数Android应用程序应该使用它,而不是生成自己的线程。 In general, Thread Pools are fairly widespread and are a great choice unless you are forced into blocking operations.
通常,线程池相当普遍,是一个不错的选择,除非您被迫阻止操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.