내 프로그램의 경우 실행중인 여러 개체에 대한 참조를 보유하기 위해 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의에서 채팅방에 액세스 할 수 있습니다 것 또는 내가 스레드 뭔가를해야 할 것 : 내 서버에서 은 내가 있나요?
이것은 작동 할 수 있습니다. 필드가 변경되지 않도록 '최종'필드를 만듭니다. –
"이것이 효과가 있을까요?"라는 말이 무슨 뜻인지 알기가 어렵습니다. –
이 줄을 실행할 때 ConcurrentHashMap이 보유 할 스레드에 대한 참조가 변경되는지는 알 수 없습니다. _chatRoomExecutor.execute (_chatRooms.get ("/ admin")); –