可以并行运行多少个线程? [英] How many threads can run in parallel?

查看:110
本文介绍了可以并行运行多少个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Java 中的多线程和并发.阅读大量关于堆栈溢出和在线的帖子.但没有清除.所以请耐心等待.

I'm learning about Multi-threading and concurrency in Java. Read lots of the posts on stack-overflow and online. But not cleared. So please bear with me.

首先我的疑问是,有人说可以同时运行的线程数等于(n=处理器核心数)对吗?我真的不确定这个声明.

First my doubt is, some people say number of threads can run in concurrently is equivalent to (n= number of processor cores) Is it correct ? I'm really not sure about this statement.

但我读到我们可以在 JVM 中创建任意数量的线程,这仅取决于我们分配给 JVM 的内存.正确吗?

But I have read that we can create as much threads we want in JVM, it only depends upon the memory we have allocated to JVM. Is it correct ?

我们应该如何决定一个java程序并行运行的线程数?是否与处理器内核的某个地方有关?

How should we decide number of threads to run in parallel for one java program ? Is it related to somewhere the processor cores ?

推荐答案

可以并行运行的线程数相当于(n=处理器内核)

number of threads can run in parallel is equivalent to (n= number of processor cores)

是的,这个说法是正确的.

Yes this statement is true.

但是我读到我们可以在 JVM 中创建任意数量的线程,它只取决于我们分配给 JVM 的内存.正确吗?

But I have read that we can create as much threads we want in JVM, it only depends upon the memory we have allocated to JVM. Is it correct ?

理论上是的.您可以在 JVM 中创建任意数量的 Threads,直到出现 OutOfMemoryError.但是创建 Threads 是一项昂贵的任务,因此作为最佳实践,请考虑使用共享线程池.这可以使用 ExecutorService 框架来实现.

Theoretically yes. You can create as many Threads you need in JVM until you get OutOfMemoryError. But creation of Threads is an expensive task so as a best practice consider using a shared pool of threads instead. This can be achieved using ExecutorService framework.

这篇关于可以并行运行多少个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆