开发者

Java Beginner: cost of threading over serial

开发者 https://www.devze.com 2023-02-13 16:12 出处:网络
I was wondering If I have(N) Double vectors, each with large length say 10,000 and I wanted to thread the开发者_StackOverflow operation(multiply by 3 for each vector ) which by making (N) threads.

I was wondering If I have (N) Double vectors, each with large length say 10,000 and I wanted to thread the开发者_StackOverflow operation(multiply by 3 for each vector ) which by making (N) threads.

I was wondering If there was a cost of using parallel (N) threads over Serial one by one operations ? Cost( memory, speed, etc) ?

or using threading is actually better idea since I read it would use available cores ?


Yes: Each thread will use up resources: at least memory, but possibly also OS processes or other OS resouces. The details will depend on the JVM implementation.

If the memory usage becomes to high you might also take a performance hit due to more frequent GC, paging and what ever computers do to manage their memory


When performing memory intensive operations, the size of your cache is more likely to be important than the size of your main memory. As the lower caches are shared, intensive use of this one cache may not give you much scalability when using multiple cores. However if you minimise the amount of data each core is using or your operations are non-trivial you can get near linear scalability


You wouldn't want to create new threads for each vector (the overhead of creating a new thread is significant). However it may make sense to do this work in a thread pool to exploit multiple cores.

It sounds as if:

  • The calculations are large enough that you would benefit from harnessing multiple threads
  • The individual vectors are large enough that they could be used as chunks of work

If the above is true, then it may make sense to use a ThreadPoolExecutor which will enable you to send each vector to the pool of threads for processing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号