简体   繁体   English

建议在Java中同时运行几个线程?

[英]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. 有一些建议的限制,但是与跟踪它们并没有任何关系。

  1. Most multithreading comes with locking. 大多数多线程都带有锁定。 If you are using central data storage or global mutable state then the more threads you have, the more lock contention you will get. 如果您正在使用中央数据存储或全局可变状态,则拥有的线程越多,锁争用就会越多。 This is app-specific and depends on how much of said state you have and how often threads read and write it. 这是特定于应用程序的,并取决于您拥有多少个上述状态以及线程读取和写入该状态的频率。
  2. There are no limits in desktop JVMs by default, but there are OS limits.It should be in the tens of thousands for modern Windows machines, but don't rely on the ability to create much more than that. 默认情况下,桌面JVM没有任何限制,但有操作系统限制,对于现代Windows计算机来说应该是成千上万个,但不要依赖于创建更多功能。
  3. Running multiple tasks in parallel is great, but the hardware can only cope with so much. 并行运行多个任务很棒,但是硬件只能应付这么多。 If you are using small threads that get fired up sometimes, and spend most their time idle, that's no biggie (Java servers were written like this for years). 如果您使用的小型线程有时会启动,并且大部分时间都闲着,那么这没什么大不了的(Java服务器这样写了很多年了)。 However if your threads are very intensive, making more of them than the number of cores you have is not likely to give you any benefit. 但是,如果您的线程非常密集,那么使它们多于您拥有的内核数就不可能给您带来任何好处。 (I believe the standard practice is twice the number of cores if you anticipate threads going idle sometimes). (我相信,如果您预计线程有时会空闲,则标准做法是内核数的两倍)。
  4. Threads have a cost to them. 线程要付出代价。 Whenever you switch Threads you switch context, and while it isn't that expensive, doing it constantly will hurt performance. 每当您切换线程时,您便会切换上下文,尽管上下文并不昂贵,但不断进行会损害性能。 It's not a good idea to create a Thread to sum up two integers and write back a result. 创建一个线程将两个整数求和并写回结果不是一个好主意。
  5. If Threads need visibility of each others state, then they are greatly slowed down, since a lot of their writes have to be written back to main memory. 如果线程需要彼此可见的状态,则它们会大大减慢速度,因为它们的许多写操作都必须写回到主内存中。 Threads are best used for standalone tasks that require little interaction with each other. 线程最适合用于相互之间几乎不需要交互的独立任务。

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.

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