我有两门以下的课。不同的客户机调用TestThreadPool,而后者又调用TestThreadExecutor来访问ExecutorService。所以问题是,ThreadPool从来没有被启动过,因为它总是创建一个新的TestThreadExecutor。
因此,我想将executeThreadExecutor设置为启动threadPool的静态方法。是否存在静态方法和静态ExecutorService的问题?
有没有其他的替代方式,比如独生子女等等?
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);
}
...
}
}发布于 2012-03-05 16:20:20
您的方法似乎没有问题,而且没有问题,虽然我不认为有任何理由使ExecutorService公开,因为您是通过类的一个方法来管理它,这对于封装来说更好。当然,您可以将TestThreadExecutor实现为单例:
public class TestThreadExecutor {
private ExecutorService testService = Executors.newFixedThreadPool(15);
private static TestThreadExecutor instance = new TestThreadExecutor();
private TestThreadExecutor() {
}
public static TestThreadExecutor getInstance() {
return instance;
}
}https://stackoverflow.com/questions/9569865
复制相似问题