Skip to content

Commit

Permalink
Use concurrent collection for the thread safety.
Browse files Browse the repository at this point in the history
  • Loading branch information
Takashi Matsuo committed Sep 12, 2013
1 parent 6a123c5 commit 35e5275
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger;

import com.google.appengine.api.NamespaceManager;
Expand Down Expand Up @@ -123,12 +123,12 @@ public static class ChatServerBridge implements Runnable {

private Map<String, Set<String>> globalParticipantsMap;

private List<Key<ChatRoomParticipants>> chatRoomParticipantsKeyList;
private CopyOnWriteArrayList<Key<ChatRoomParticipants>> chatRoomParticipantsKeyList;

private ChatServerBridge() {
namespace = NamespaceManager.get();
globalParticipantsMap = new HashMap<>();
chatRoomParticipantsKeyList = new ArrayList<>();
globalParticipantsMap = new ConcurrentHashMap<>();
chatRoomParticipantsKeyList = new CopyOnWriteArrayList<>();
}

/**
Expand Down Expand Up @@ -191,8 +191,8 @@ public void stop() {
// delete participant list in the datastore
ofy().delete().keys(chatRoomParticipantsKeyList).now();
// initialize variables
globalParticipantsMap = new HashMap<>();
chatRoomParticipantsKeyList = new ArrayList<>();
globalParticipantsMap = new ConcurrentHashMap<>();
chatRoomParticipantsKeyList = new CopyOnWriteArrayList<>();
chatSocketServer = null;
} catch (IOException|InterruptedException e) {
LOG.warning(Throwables.getStackTraceAsString(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
import com.google.common.collect.ImmutableSet;
import org.java_websocket.WebSocket;

import java.util.*;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* A class that manages connections from the clients.
Expand Down Expand Up @@ -80,8 +84,8 @@ private static String createIdFromConnection(WebSocket connection) {
* Creates a MetaInfoManager with the initialized map objects.
*/
public MetaInfoManager() {
connectionMap = new HashMap<>();
participantMap = new HashMap<>();
connectionMap = new ConcurrentHashMap<>();
participantMap = new ConcurrentHashMap<>();
}

/**
Expand Down

0 comments on commit 35e5275

Please sign in to comment.