ExecutorService slow multi thread performance

后端 未结 3 1901
暗喜
暗喜 2021-01-12 17:09

I am trying to execute a simple calculation (it calls Math.random() 10000000 times). Surprisingly running it in simple method performs much faster than using Ex

3条回答
  •  爱一瞬间的悲伤
    2021-01-12 17:30

    You'd do well to read the contents of the other thread. There's plenty of good tips in there.

    Perhaps the most significant issue with your benchmark is that according to the Math.random() contract, "This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator"

    Read this as: the method is synchronized, so only one thread is likely to be able to usefully use it at the same time. So you do a bunch of overhead to distribute the tasks, only to force them again to run serially.

提交回复
热议问题