multithreading - How can I improve performance with FutureTasks -


the problem seems simple, have number (huge) of operations need work , main thread can proceed when of operations return results, however. tried in 1 thread , each operation took let's 2 10 seconds @ most, , @ end took 2,5 minutes. tried future tasks , submited them executorservice. of them processed @ time, each of them took let's 40 150 seconds. in end of day full process took 2,1 minutes.

if i'm right, threads nothing way of execute @ once, although sharing processor's power, , thought processor working heavily me tasks executed @ same time taking same time take excecuted in single thread.

question is: there way can reach this? (maybe not future tasks, maybe else, don't know)

detail: don't need them work @ same time doesn't matter me matters performance

you might have created way many threads. consequence, cpu switching between them generating noticeable overhead.

you need limit number of running threads , can submit tasks execute concurrently.

something like:

executorservice es = executors.newfixedthreadpool(8); list<future<?>> futures = new arraylist<>(runnables.size()); for(runnable r : runnables) {     es.submit(r); } // wait finish: for(future<?> f : futures) {     f.get(); } // done 

Popular posts from this blog