对于我的程序,我使用 ConcurrentHashMap 来保存对多个正在运行的对象的引用。在我的服务器中,我有:
public class Server {
private ConcurrentHashMap<String, ChatRoom> _chatRooms;
private ExecutorService _chatRoomExecutor;
...
// create a new channel executor to handle 50 chatrooms
_chatRoomExecutor= Executors.newFixedThreadPool(50);
// create an admin chatroom for testing at this point
_chatRooms.put("/admin", new Channel("/admin"));
// execute that chatroom
_chatRoomExecutor.execute(_chatRooms.get("/admin"));
这会起作用吗,因为我仍然可以从 ConcurrentHashMap 访问聊天室,还是我必须对线程池做些什么?