ExecutorService slow multi thread performance

后端 未结 3 1932
暗喜
暗喜 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:19

    Math.random() is synchronized. Kind of the whole point of synchronized is to slow things down so they don't collide. Use something that isn't synchronized and/or give each thread its own object to work with, like a new Random.

提交回复
热议问题