0

I am developing a standalone java based application which is supposed to be run on at least a quad core machine. This application takes PDF files as an input and creates an Epub package as an output, now client has asked me to increase the performance of this application using multiple cores. Now I have created one thread for each PDF file to be processed, and on a quad core machine pool of four threads will be opened at a time which increase the performance of whole process by 50%.

But i am not satisfied with the performance, as i think performance can be increase by 80% at least as on single thread application it was producing 10 files in a minutes, and now it is only producing 20 files in a minute on multithreded application where as it should have produced 40 files in a minute as now it is running in four threads at a time.

Kindly suggest what should I follow to achieve the optimal result, is there anything wrong in the above approach? Also i am looking for suggestions that how many concurrent threads can be opened on a quad core machine?

  • 2
    You're going to have to post your code. Or at least a minimal reproducible example. – Jason May 19 '20 at 11:24
  • 3
    It is very hard to say anything definitive about this, and the best way to find out is just to test it and measure the results. Note that it might very well be that the speed of your application is limited by I/O, in which case adding more threads will not help. But the only way to know for sure, is by measuring. – Jesper May 19 '20 at 11:24
  • "where as it should have produced 40 files in a minute as now it is running in four threads at a time", that's not how threading works. – Kayaman May 19 '20 at 11:32
  • @Jason, actually code is huge and lots of modules are there. I have followed all possible optimization, but still not sure that how many concurrent threads can be opened on a quad core machine, is there any rule to identify this? – Suman Saurav May 19 '20 at 11:41
  • 1
    I support @Jesper 's idea of testing and measuring the actual behaviour. It highly depends on where your thread is waiting for. If it is mostly dependant on IO then adding more thread won't help much. However if the thread does a lot of CPU calculations, then adding more threads and testing will give you better results. – codetiger May 19 '20 at 11:52
  • @codetiger, yes there are few IO operations but i have monitored these and it is not hampering the overall process. One thing i have noted that different no of thread are producing different performance, and i am not sure that how many concurrent threads on one core will give best perfomance. – Suman Saurav May 19 '20 at 12:00
  • 1
    @SumanSaurav, Usually as a thumb rule, for CPU heavy operations with no IO operations, it is good to go with 1 thread per core. With more IO operations involved, it depends on the wait time. Coz increasing more than a certain number will introduce a race condition. – codetiger May 19 '20 at 12:12
  • This answer covers it well. https://stackoverflow.com/questions/1718465/optimal-number-of-threads-per-core – codetiger May 19 '20 at 12:14
  • @Codetiger yes, i have used one thread per core, but there two types of core, one virtual core other physical core, in my machine it is 8 virtual core while only 4 physical cores are available, what should i choose in that scenario? – Suman Saurav May 19 '20 at 13:00
  • Re, "there are few IO operations but i have monitored these..." What does that mean, "you have _monitored_...?" What percent of the total wall-clock time does each thread in your program spend waiting for I/O? And how does that change when you add more threads? If all of the files are on the same file system, then adding more threads is unlikely to improve I/O performance. As for overall performance, read about _[Amdahl's Law](https://en.wikipedia.org/wiki/Amdahl%27s_law)_. In a nutshell, even a small bottleneck can severely limit the performance improvement that you get by using more threads. – Solomon Slow May 19 '20 at 13:15
  • @SumanSaurav It is safe to go with virtual core count, the chip will take care of managing the multiplexing it into the physical core efficiently. – codetiger May 20 '20 at 05:28

0 Answers0