9

Can a single thread of a Java program automatically make use of multiple cores on the CPU?

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
user697911
  • 10,043
  • 25
  • 95
  • 169
  • Not to answer a question with a question, but how could one check-out teller make use of multiple, empty check-out booths? – Makoto Apr 10 '13 at 20:58

3 Answers3

15

Can a single thread of a Java program automatically make use of multiple cores on the CPU?

Yes and no. A single threaded Java program will use multiple threads in that the GC, JMX, finalizer, and other background threads can run in different CPUs (whether CPU or core). The GC threads especially give a significant performance boost if they can be running in another CPU. However, your single threaded application code, although it may move between CPUs, will never be running in 2 CPUs at the same time.

how to find out that?

That's a harder question and it depends on what architecture you are running on. ps under *nix will be able to show if you have multiple threads in the run queue but even then it may not show that they are actually executing in multiple CPUs.

Gray
  • 115,027
  • 24
  • 293
  • 354
1

Your own code will not run on multiple cores if it is by definition single threaded. No single threaded application can run simultaneously on multiple cores - unless your using underluing multithreaded calls/libraries without knowing.

Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • So to make my application faster, i have to make it multiple threaded? My machine has 16 cores, and one core is indicated that has used over 100% cpu, but others are free. and the application is extremely slow. – user697911 Apr 10 '13 at 21:15
  • Yes, you need to figure out how to write your algorithm so that the work is split between the threads. For example, if your using a for loop doing something to an array of 160 elements, you could spawn 16 threads that work on 10 elements each. However, you need to be sure that previous calculations do not require future calculations. For example, if your calculation the total profit over 160 day, each day would require calculating the previous. – Menelaos Apr 10 '13 at 21:22
  • Nope, memory usage should not increase very much. Ofcourse, it depends on how you code your solution, and how much data each thread will require. However, if you pass a reference to the same data as you were originally using it should be a negligible difference. Is this for some type of data analysis, calculation etc? – Menelaos Apr 10 '13 at 21:27
  • Data analysis line by line in a text file, and the text file is pretty big. – user697911 Apr 10 '13 at 21:31
  • As the analysis most likely takes longer than the file reading, you could have your main thread reading lines from the file and entering them into a queue until a maximum amount of entries are reached. At the same time you could have 15 threads pulling a line each time and doing analysis. This would be a 1 producer, multiple consumer pattern e.g. http://stackoverflow.com/questions/3119692/how-to-implement-one-producer-multiple-consumers-and-multiple-objects-in-java and http://stackoverflow.com/questions/3119692/how-to-implement-one-producer-multiple-consumers-and-multiple-objects-in-java – Menelaos Apr 10 '13 at 21:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27985/discussion-between-menelaos-and-user697911) – Menelaos Apr 10 '13 at 21:39
0

Usually gc is running in a separate thread. But usually it doesn't make any significant difference. That's all.

kan
  • 28,279
  • 7
  • 71
  • 101
  • The GC running in a separate thread is a _huge_ performance improvement in my experience. – Gray Apr 10 '13 at 20:59
  • @Gray It depends on type of program - how objects are created, how many and how long do they live. – kan Apr 10 '13 at 21:03
  • Indeed. But to say that it doesn't make "any significant difference" is IMHO, incorrect. – Gray Apr 10 '13 at 21:05
  • @kan if you ever try to implement a low-latency application running on 60+ cores in 20+GB, you will love those concurrent/parallel GCs – Ralf H Apr 10 '13 at 21:56
  • Like Gray, I was referring to the idea of _GC running in a separate thread_ making no difference. – Ralf H Apr 11 '13 at 08:44