Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am running a program that is supposed to solve a problem with the brute force approach. My computer has a quad-core CPU. When I run the program clearly java uses only one core, because in my program I use only one thread. Is there a way to split up the program in more threads so that more core's are used and so to improve calculation speed? I mean: if one has a program which has a natural solution that uses only one thread, is it possible to force it use more threads without completely changing the code?

share|improve this question
6  
Some problems are “embarrassingly parallel”, which are trivial to parallelize. This could be the case when your algorithm can be easily expressed in a pure functional manner. Other problems lend themselves to concurrency patterns like producer-consumer or map-reduce. However, multithreading is difficult and there is no silver bullet – you will likely see better results by refining your brute force approach (e.g. using heuristics, dynamic programming, or memoization). What technique is appropriate depends on the specific problem. –  amon Mar 29 at 15:10
2  
It won't use multiple threads magically. Some refactoring of your code will be needed, but how much depends entirely on the structure of your program and the algorithm. –  Bart van Ingen Schenau Mar 29 at 20:00
    
Well, this sounds like the kind of work JDK 8 Parallel Streams were created to solve. –  edalorzo Mar 30 at 20:37
    
The quick and dirty approach is to run multiple instances of your program with different subsets of the input. Whether this is practical depends on your application. –  Brian Mar 30 at 22:38
1  
I'd start reading up on concurrent programming (CP) if I were you. CP is a very broad topic that you can take quite a bit of time to master. If you haven't got any clue, start with the basics. There's quite a bit of theory to master to get a grip on the matter. A book like Modern Operating Systems by A. Tanenbaum is a good foundation to build on. en.wikipedia.org/wiki/Modern_Operating_Systems –  Onno Mar 31 at 1:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.