【发布时间】:2015-09-05 14:15:24
【问题描述】:
有 7-8 个类(可调用的实现)具有一些相似的行为,即它们具有一些具有相似实现的相似功能。而且所有这些都使用了所有这些类都相同的 HashMap(仅用于阅读目的)。 所以我决定创建一个抽象超类,包含所有类似的方法加上这个 hashMap 作为静态成员。 我将为这 7-8 个可调用类创建子类(因此这些类也可以通过继承调用),以便提高应用程序的性能。
现在我有 3 个查询:
1.) 这个设计有什么缺陷吗?我可以进一步改进它吗?
2.) 是否会出现任何并发问题,因为它是一个三层层次结构,可调用类位于底部两层?
3.) 在静态块中初始化静态成员(哈希图)是否错误?因为我的老板非常反对使用静态成员和块。那么如果我在静态块中初始化这个地图会出现什么问题呢?
public abstract class AbSuper {
private static HashMap hmap;
private static CompletionService<String> service;
private static int maxThreads = 10;
static{
initializeMap();
}
public static void initializeMap(){
//load from file
}
public HashMap getmap(){
return hmap;
}
public void commonMethodOne(){
//do something
}
public static CompletionService<String> getService(){
ExecutorService executor = Executors.newFixedThreadPool(maxThreads);
service = new ExecutorCompletionService<String>(executor);
return service;
}
}
public class CallableOne extends AbSuper implements Callable<String>{
private List<String[]> taskList;
protected HashMap resultMap;
public List<String[]> getTaskList(){
return taskList;
}
public String call(){
for(String[] task : getTaskList()){
getService().submit(new SubCallableOne(task));
}
return "done with Callable One";
}
}
public class SubCallableOne extends CallableOne {
String[] task;
public SubCallableOne(String[] task) {
this.task = task;
}
public String call(){
//do what you are suppose to do
//and then access and populate "resultMap" fom superclass
return "done with subCallableOne";
}
}
会有7-8个CallableOne/二/三和它们对应的SubCallableOne/二/三。
【问题讨论】:
-
您能否提供一些与您的问题相关的代码? stackoverflow.com/help/how-to-ask
标签: java inheritance concurrency static