用非功能语言编写功能程序

时间:2022-04-11 15:04:07

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?

假设我在Java中使用不可变数据结构编写程序。即使它不是一种功能语言,它也应该能够并行执行。如何确保使用处理器的所有内核执行程序?计算机如何决定可以并行运行哪些代码?

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?

附:我提出这个问题的意图不是要找出如何并行化java程序。但要知道 - 计算机如何并行化代码。它可以在用非功能语言编写的功能程序中完成吗?

5 个解决方案

#1


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.

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

#2


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.

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

#3


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

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

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

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

#4


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)

您可以编写具有自动并行任务的函数,对于特定情况可以很容易地执行,但是我不知道有任何内置的Java API可以执行此操作。 (可能是Executor / ExecutorService除外)

#5


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.

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

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

它是否对你有用是一个不同的问题:-)

#1


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.

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

#2


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.

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

#3


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

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

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

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

#4


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)

您可以编写具有自动并行任务的函数,对于特定情况可以很容易地执行,但是我不知道有任何内置的Java API可以执行此操作。 (可能是Executor / ExecutorService除外)

#5


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.

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

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

它是否对你有用是一个不同的问题:-)