ExecutorService疑问?

miracleliu 2015-02-10 02:58:41
我有3个不同的线程要执行,
定义三个不同的threadPool?还是只定义一个threadPool?

方案一:

public class ThreadExe {

public static final ExecutorService executor1 = Executors.newFixedThreadPool(24);
public static final ExecutorService executor2 = Executors.newFixedThreadPool(24);
public static final ExecutorService executor3 = Executors.newFixedThreadPool(24);
}

调用的类:
ThreadExe.executor1(new Thead1());
ThreadExe.executor2(new Thead2());
ThreadExe.executor3(new Thead3());



方案二:


public class ThreadExe {
public static final ExecutorService executor = Executors.newFixedThreadPool(24);
}

调用的类:
ThreadExe.executor(new Thead1());
ThreadExe.executor(new Thead2());
ThreadExe.executor(new Thead3());




这2个方法哪个更好?或者有什么特定使用场景?
...全文
162 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_22763113 2015-02-10
  • 打赏
  • 举报
回复
默默的来了默默的走了。默默
  • 打赏
  • 举报
回复
看你线程具体干的什么活了。。。 如果有明显的区别,比如一个是运算密集型的,一个是io密集型的,或者一个是短任务,一个是长任务。 可以考虑针对不同的线程,定义多个线程池。否则的话,没有太大的必要。
fengdavid 2015-02-10
  • 打赏
  • 举报
回复
线程池的作用就是为将要执行的任务提供线程,提高效率,因为至少在池中保持核心线程数量的线程不销毁(是创建时指定的参数,如楼主这里的24)。一般情况下在一个应用中只需要一个池,由池去维护应用中用到的线程。多个池的情况,会在数据库、中间件等大型应用用到

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧