Service中如何使用線程池

通常都是採用新建業務類implements Runnable接口的方式。
但是在service中這樣寫反而不便。

一種簡便的方法是在service中直接加入代碼:

private static ExecutorService threadPoolForFailed = Executors.newFixedThreadPool(10);

然後用execute或者submit方法,實測可用。

threadPoolForFailed.execute(new Runnable() {
    @Override
    public void run() {
       // TODO 
    }
});

另外一種方法

通過Aware,較麻煩,在此不敘述。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章