2015-10-23 14 views
0

我讀Java併發實踐,這裏面的東西我搞糊塗了:newSingleThreadExecutor是否返回由unconfigurableExecutorService包裝的ExecutorService?

執行人包括工廠方法,unconfigurableExecutorService,這需要現有的ExecutorService和一個包裝它僅僅曝光方法的ExecutorService,所以它不能進一步配置。與合併實現不同,newSingleThreadExecutor返回一個以此方式打包的ExecutorService,而不是原始的ThreadPoolExecutor。

我讀過newSingleThreadExecutor的源代碼,但就是怎麼也找不到它是由unconfigurableExecutorService包裹?

回答

2

unconfigurableExecutorService方法

newSingleThreadExecutor返回的值不包含unconfigurableExecutorService,但類似的東西。它javadoc狀態

與其他等效newFixedThreadPool(1)返回 執行人保證無需重新配置使用其他 線程。


只需要使用適當的封裝完成。 newFixedThreadPool返回ThreadPoolExecutor,實現ExecutorService的類。你可以把結果和使用它的方法

ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1); 
executor.setCorePoolSize(4); 

你不能這樣做,隨着newSingleThreadExecutor的結果,因爲它不返回ThreadPoolExecutor,它返回一個實現ExecutorService一些其他類。

相關問題