我已经编写了一个任务侦听器模型作为下面的代码,是否可以对其进行任何优化或错误修复(如果有),尤其是列表和地图访问?在这个模型中,创建一个 Map 来保存多个 List,其中包含一些用户的监听器,每个 List 的大小大多在 1000 左右。这里有两点: 1. 为了性能,使用 HashMap 还是 ConcurrentHashMap?2.这是否比复制 List 并在多线程访问上迭代复制的 List 更好的方法?
public class TaskActionManager {
private static final Map<String, List<TaskActionListener>> listenersMap = new ConcurrentHashMap<String, List<TaskActionListener>>();
private static final ExecutorService executor = Executors
.newCachedThreadPool();
private static ReentrantLock lock = new ReentrantLock();
public static final void addListener(TaskActionListener listener,
TaskActionType type) {
// type is an enum
String key = type.name();
List<TaskActionListener> list = listenersMap.get(key);
lock.lock();
try {
// Mostly the list is not null
if (list == null) {
list = listenersMap.get(key);
if (list == null) {
list = new ArrayList<TaskActionListener>();
listenersMap.put(key, list);
}
}
list.add(listener);
} finally {
lock.unlock();
}
}
public static final void removeListener(TaskActionListener listener,
TaskActionType type) {
List<TaskActionListener> list = listenersMap.get(type.name());
if (list == null)
return;
lock.lock();
try {
list.remove(listener);
} finally {
lock.unlock();
}
}
public static final void fireAction(final TaskAction action) {
executor.execute(new Runnable() {
@Override
public void run() {
List<TaskActionListener> list = listenersMap.get(action
.getType().name());
if (list == null)
return;
// Make a copy
List<TaskActionListener> copy = null;
lock.lock();
try {
copy = new ArrayList<TaskActionListener>(list.size());
Collections.copy(list, copy);
} finally {
lock.unlock();
}
// Iterate the copy
if (copy != null) {
for (TaskActionListener listener : copy) {
try {
listener.fireAction(action);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
});
}
}