Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to call below calculation() simultaneously in calculateKalmanValues() for each element in the 'movingBeacons' list to reduce the processing time. I think java stream.parallel() is the ideal solution.
Java
public void calculation(){
           // do some think
       }

// This is the method which I call calculation()
//for each element in the  movingBeacons list simultaneously should called the calculation method

   public void calculateKalmanValues()  {
       List<string> movingBeacons=incomingBtRssiRepository.movingBeacons();

       movingBeacons.forEach.parallel()?????
By using java stream.parallel() or multithreading.

What I have tried:

I tried using java stream.parallel() method but I was unable to configure it correctly,
what I want is to avoid calling method one by one and parallelly call and run the method for all the elements in the list.
Posted
Updated 3-May-19 6:11am
v2

1 solution

You can't - or at least it's very unlikely that you can unless the list is very short.

Threading isn't a "Magic bullet" that immediately makes everything faster. Each thread requires a free core to run on, and when the number of threads in the whole system exceeds the number of cores, threads get suspended waiting for a core to be free. So in an ideal world, you can run the same number of your calculations simultaneously as you have cores in yoru processor. But ... this isn't ideal, the OS will be running N threads at the same time to do "OS related" processing, and those threads also use the same set of cores.

In practice, if you generate too many threads you may even slow down processing because both creating, switching, and terminating threads requires cores, and it takes time - and each thread requires it's own stack, so it uses more memory as well, and if that exceed the physical memory available to the whole system, then it starts to get paged out to disk and that alone will slow your code by massive amounts.

Try really hard not to generate more threads running "concurrently" than you need - it can have a serious performance impact on the whole system, not just your app.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900