用非函数式语言编写函数式程序

发布于 2024-07-19 03:35:55 字数 200 浏览 7 评论 0原文

假设我用 Java 编写一个使用不可变数据结构的程序。 尽管它不是函数式语言,但它应该能够并行执行。 如何确保我的程序正在使用处理器的所有核心执行? 计算机如何决定哪些代码可以并行运行?

PS 我问这个问题的目的不是为了找出如何并行化 java 程序。 但要知道 - 计算机如何并行化代码。 它可以在用非函数式语言编写的函数式程序中做到这一点吗?

Suppose I write a program using immutable data structures in Java. Even though it is not a functional language, it should be able to execute parallely. How do I ensure that my program is being executed using all the cores of my processer? How does the computer decide which code can be run parallely?

P.S. My intent in asking this question was not to find out how to parrallelize java programs. But to know - how does the computer parallelize code. Can it do it in a functional program written in a non functional language?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

御弟哥哥 2024-07-26 03:35:55

Java 程序通过线程并行化。 计算机无法神奇地弄清楚如何使用 Java 等命令式语言将应用程序的各个部分分布到所有内核上。 只有像 Erlang 或 Haskell 这样的函数式语言才能做到这一点。 阅读 Java 线程。

Java programs are parallelized through threads. The computer can't magically figure out how to distribute the pieces of your application across all the cores in an imperative language like Java. Only a functional language like Erlang or Haskell could do that. Read up on Java threads.

友欢 2024-07-26 03:35:55

我不知道自动并行化 JVM。 它们确实存在于其他语言中,例如 FORTRAN。

您可能会发现为 JDK7 计划的 JSR166y fork-join 框架 很有趣。

I am not aware of automatic parallelization JVMs. They do exist for other languages such as FORTRAN.

You might find the JSR166y fork-join framework scheduled for JDK7 interesting.

娜些时光,永不杰束 2024-07-26 03:35:55

我不认为你可以“强制”JVM并行化你的程序,但是有一个单独的线程执行每个“任务”,如果你能以这种方式分解你的程序,在大多数情况下可能会成功? 然而并行性仍然不能得到保证。

i dont think you can "force" the JVM to parallelize your program, but having a separate thread executing each "task", if you can break down your program that way, would probably do the trick in most cases? parallelism is still not guaranteed however.

冷默言语 2024-07-26 03:35:55

您可以编写具有自动并行任务的函数,对于特定情况来说这相当容易,但是我不知道有任何内置的 Java API 可以做到这一点。 (Executor/ExecutorService 可能除外)

You can write functions with automatically parallelise tasks, it is fairly easy to do for specific cases, however I am not aware of any built-in Java API which does this. (Except perhaps the Executor/ExecutorService)

故事与诗 2024-07-26 03:35:55

我在学校用过的东西为你做了很多工作。

http://www.cs.rit.edu/~ark/pj.shtml< /a>

它具有执行 SMP 或基于消息的并行性的能力。

它对您是否有用是另一个问题:-)

Something that I used in school that did alot of the work for you.

http://www.cs.rit.edu/~ark/pj.shtml

It has the capability to do SMP or message based parallelism.

Whether or not it is useful to you is a different question :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文