views:

544

answers:

5

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?

+8  A: 

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.

musicfreak
I don't see why that should be the preserve of "pure functional" languages.
Tom Hawtin - tackline
You're right, I don't know why I put that there...
musicfreak
A program using immutable structures in Erlang or Haskell doesn't parallelize magically either. The parallelization tools are much easier to use, but you still have to explicitly use them.
Nathan Sanders
@Nathan Sanders: I said *could*, aka it would be easy to write such a program/framework in those languages. Doing so in Java would take a lot more work, and it would be a lot less elegant.
musicfreak
@musicfreak: Can you please explain this part "Only a functional language like Erlang or Haskell could do that" What is so special about functional languages. Actually, I do not understand why Java cannot do it in first place. Thanks!
Lazer
@Lazer: You cannot automatically parallelize a procedural language like Java because the compiler has no way of knowing what code can be spread across CPU cores. Any function may or may not have side effects. In functional language, everything is immutable, which makes being "thread-safe" much easier, and allows you to write functions that can automatically parallelize (think Map/Reduce). I'm not saying that parallel programming is impossible in Java, it's just much more painful and not even close to seamless.
musicfreak
@musicfreak: I understand now. thanks!
Lazer
+4  A: 

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.

Tom Hawtin - tackline
The automatic parallelization link was useful, thanks!
Pranav
+3  A: 

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.

Chii
+1  A: 

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)

Peter Lawrey
A: 

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

JH