Effective Java - Don’t depend on the thread scheduler

這個Item重點就是要你不要相信thread scheduler可以幫你解決thread排班問題,把code寫好比較實在。有幾個重點要注意:

  1. Thread的數量不要大於CPU的核心數。一般Thread數量都會使用CPU核心數+1的公式,但在Java Concurrency In Practice中,有提供根據CPU bound vs IO bound比率去tune thread pool size的方法,大家可以參考。
  2. 基於不可靠、不好測試與不好移植的原因,作者建議大家別用Thread.yield還有控制Thread priority去解決排班問題。

Effective Java第三版Item 84。