11

I was just wondering whether we actually need the algorithm to be muti-threaded if it must make use of the multi-core processors or will the jvm make use of multiple core's even-though our algorithm is sequential ?

UPDATE:

Related Question:

Community
  • 1
  • 1
Emil
  • 13,577
  • 18
  • 69
  • 108
  • 2
    With the exception of trivial academic assignments, I think most Java programs are multi-threaded anyway. I always think it is fun to hear comments from students working with Swing the first time as they encounter ConcurrentModificationException since they are forced into a multi-threaded environment without really realizing. – Tim Bender Oct 04 '10 at 20:26

4 Answers4

20

I don't believe any current, production JVM implementations perform automatic multi-threading. They may use other cores for garbage collection and some other housekeeping, but if your code is expressed sequentially it's difficult to automatically parallelize it and still keep the precise semantics.

There may be some experimental/research JVMs which try to parallelize areas of code which the JIT can spot as being embarrassingly parallel, but I haven't heard of anything like that for production systems. Even if the JIT did spot this sort of thing, it would probably be less effective than designing your code for parallelism in the first place. (Writing the code sequentially, you could easily end up making design decisions which would hamper automatic parallelism unintentionally.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Does this mean that even inbuilt function's like Arrays.sort() will have no effect even if you have multi-core processor since the sort function is not designed for parallel execution. – Emil Oct 01 '10 at 06:26
  • 1
    @Emil: Correct - unless the implementation is ever changed to use multiple threads, of course. – Jon Skeet Oct 01 '10 at 06:30
  • 4
    Even if your Java program is embarrassingly sequential, having an extra core will allow your host machine to do other work (file system, monitoring, network processing) much more efficiently. – O. Jones Oct 02 '10 at 15:28
  • Thanks for "They may use other cores for garbage collection...", now I have a clue why my threadless code uses all my processor's cores. – mulya Mar 07 '18 at 10:05
5

Your implementation needs to be multi-threaded in order to take advantage of the multiple cores at your disposal.

Your system as a whole can use a single core per running application or service. Each running application, though, will work off a single thread/core unless implemented otherwise.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
2

Java will not automatically split your program into threads. Currently, if you want you code to be able to run on multiple cores at once, you need to tell the computer through threads, or some other mechanism, how to split up the code into tasks and the dependencies between tasks in your program. However, other tasks can run concurrently on the other cores, so your program may still run faster on a multicore processor if you are running other things concurrently.

An easy way to make you current code parallizable is to use JOMP to parallelize for loops and processing power intensize, easily parellized parts of your code.

Statler
  • 324
  • 1
  • 3
  • 7
1

I dont think using multi-threaded algorithm will make use of multi-core processors effectively unless coded for effectiveness. Here is a nice article which talks about making use of multi-core processors for developers -

http://java.dzone.com/news/building-multi-core-ready-java

Sachin Shanbhag
  • 54,530
  • 11
  • 89
  • 103
  • 2
    please justify your answer. IMO, a multi-threaded algorithm will make use of multiple cores on a multi-core processor. It won't necessarily use the cores **effectively**. If the algorithm has concurrency bottlenecks, etc then you'd expect many cores to be idle for much of the time. – Stephen C Oct 01 '10 at 07:18
  • @Stephen - Thanks. Agree with you. Edited my answer to add effectiveness of multi-threaded apps. – Sachin Shanbhag Oct 01 '10 at 08:30