Pranav
Pranav

Reputation: 3312

Writing functional programs in non-functional languages

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?

Upvotes: 5

Views: 749

Answers (5)

JH.
JH.

Reputation: 4219

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 :-)

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533530

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)

Upvotes: 1

Chii
Chii

Reputation: 14738

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.

Upvotes: 3

Sasha Chedygov
Sasha Chedygov

Reputation: 130827

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.

Upvotes: 8

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

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.

Upvotes: 5

Related Questions