首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >静态ExecutorService

静态ExecutorService
EN

Stack Overflow用户
提问于 2012-03-05 16:04:21
回答 1查看 3.6K关注 0票数 0

我有两门以下的课。不同的客户机调用TestThreadPool,而后者又调用TestThreadExecutor来访问ExecutorService。所以问题是,ThreadPool从来没有被启动过,因为它总是创建一个新的TestThreadExecutor

因此,我想将executeThreadExecutor设置为启动threadPool的静态方法。是否存在静态方法和静态ExecutorService的问题?

有没有其他的替代方式,比如独生子女等等?

代码语言:javascript
复制
public class Client {
  ...
  TestThreadPool testThreadPool = new TestThreadPool ();
  ...
}

public class TestThreadPool {
  public void executeThread() {
    TestThreadExecutor  test =  new TestThreadExecutor();
    ...
  }
}

public class TestThreadExecutor {
  public static ExecutorService testService = null;

  public static void executeThreadExecutor {
    if (testService == null) {      
      testService = Executors.newFixedThreadPool(15);
    }
    ...
  } 
}
EN

回答 1

Stack Overflow用户

发布于 2012-03-05 16:20:20

您的方法似乎没有问题,而且没有问题,虽然我不认为有任何理由使ExecutorService公开,因为您是通过类的一个方法来管理它,这对于封装来说更好。当然,您可以将TestThreadExecutor实现为单例:

代码语言:javascript
复制
public class TestThreadExecutor {
   private ExecutorService testService = Executors.newFixedThreadPool(15);
   private static TestThreadExecutor instance = new TestThreadExecutor();

   private TestThreadExecutor() {
   }

   public static TestThreadExecutor getInstance() {
       return instance;
   }        
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9569865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档