Skip to content

Commit

Permalink
Fix concurrent modification exception
Browse files Browse the repository at this point in the history
  • Loading branch information
neelam-kushwah committed Jun 7, 2024
1 parent 3b9cf78 commit b23e2fe
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -215,19 +216,23 @@ static void sendTopicUpdateToHost(UMessage umessage) {
jsonObj.put(Constants.ACTION_DATA, serializedMsg);
List<Socket> socketList = Constants.TOPIC_SOCKET_MAP.get(topic);
if (socketList != null) {
socketList.forEach(socket -> {
try {
PrintWriter wr = new PrintWriter(socket.getOutputStream());
wr.println(jsonObj);
wr.flush();
Log.d(LOG_TAG, "Sending topic update to host: " + jsonObj);

} catch (IOException e) {
e.printStackTrace();
// Log the exception and continue with the next socket
Log.e(LOG_TAG, "Exception occurs while sending topic update to host", e);
synchronized (socketList) {
Iterator<Socket> iterator = socketList.iterator();
while (iterator.hasNext()) {
Socket socket = iterator.next();
try {
PrintWriter wr = new PrintWriter(socket.getOutputStream());
wr.println(jsonObj);
wr.flush();
Log.d(LOG_TAG, "Sending topic update to host: " + jsonObj);

} catch (IOException e) {
e.printStackTrace();
// Log the exception and continue with the next socket
Log.e(LOG_TAG, "Exception occurs while sending topic update to host", e);
}
}
});
}
}

} catch (JSONException e) {
Expand Down

0 comments on commit b23e2fe

Please sign in to comment.