potential concurracy error fix

This commit is contained in:
sirjonasxx 2018-10-16 01:17:52 +02:00
parent c620d84c2e
commit 5b11349133

View File

@ -148,13 +148,17 @@ public class Extensions extends SubForm {
getHConnection().addStateChangeListener((oldState, newState) -> { getHConnection().addStateChangeListener((oldState, newState) -> {
if (newState == HConnection.State.CONNECTED) { if (newState == HConnection.State.CONNECTED) {
for (GEarthExtension extension : gEarthExtensions) { synchronized (gEarthExtensions) {
extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.CONNECTIONSTART)); for (GEarthExtension extension : gEarthExtensions) {
extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.CONNECTIONSTART));
}
} }
} }
if (oldState == HConnection.State.CONNECTED) { if (oldState == HConnection.State.CONNECTED) {
for (GEarthExtension extension : gEarthExtensions) { synchronized (getHConnection()) {
extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.CONNECTIONEND)); for (GEarthExtension extension : gEarthExtensions) {
extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.CONNECTIONEND));
}
} }
} }
}); });
@ -172,7 +176,17 @@ public class Extensions extends SubForm {
boolean[] isblock = new boolean[1]; boolean[] isblock = new boolean[1];
for (GEarthExtension extension : collection) { Iterator<GEarthExtension> it;
synchronized (collection) {
it = collection.iterator();
}
while (true) {
GEarthExtension extension;
synchronized (collection) {
if (!it.hasNext()) break;
extension = it.next();
}
GEarthExtension.ReceiveMessageListener respondCallback = new GEarthExtension.ReceiveMessageListener() { GEarthExtension.ReceiveMessageListener respondCallback = new GEarthExtension.ReceiveMessageListener() {
@Override @Override
public void act(HPacket packet) { public void act(HPacket packet) {
@ -196,22 +210,26 @@ public class Extensions extends SubForm {
} }
}; };
extension.addOnReceiveMessageListener(respondCallback); extension.addOnReceiveMessageListener(respondCallback);
extension.sendMessage(manipulatePacketRequest); extension.sendMessage(manipulatePacketRequest);
} }
//block untill all extensions have responded //block untill all extensions have responded
List<GEarthExtension> willdelete = new ArrayList<>(); List<GEarthExtension> willdelete = new ArrayList<>();
while (!collection.isEmpty()) { while (true) {
synchronized (collection) { synchronized (collection) {
if (collection.isEmpty()) {
break;
}
for (GEarthExtension extension : collection) { for (GEarthExtension extension : collection) {
synchronized (gEarthExtensions) { synchronized (gEarthExtensions) {
if (!gEarthExtensions.contains(extension)) willdelete.add(extension); if (!gEarthExtensions.contains(extension)) willdelete.add(extension);
} }
} }
for (int i = willdelete.size() - 1; i >= 0; i--) { for (int i = willdelete.size() - 1; i >= 0; i--) {
collection.remove(willdelete.get(i)); synchronized (collection) {
collection.remove(willdelete.get(i));
}
willdelete.remove(i); willdelete.remove(i);
} }
} }
@ -259,7 +277,9 @@ public class Extensions extends SubForm {
} }
} }
}; };
messageListeners.put(extension, receiveMessageListener); synchronized (messageListeners) {
messageListeners.put(extension, receiveMessageListener);
}
extension.addOnReceiveMessageListener(receiveMessageListener); extension.addOnReceiveMessageListener(receiveMessageListener);
extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.INIT)); extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.INIT));
@ -283,8 +303,10 @@ public class Extensions extends SubForm {
gEarthExtensions.remove(extension); gEarthExtensions.remove(extension);
} }
synchronized (messageListeners) {
extension.removeOnReceiveMessageListener(messageListeners.get(extension)); extension.removeOnReceiveMessageListener(messageListeners.get(extension));
messageListeners.remove(extension); messageListeners.remove(extension);
}
Platform.runLater(extension::delete); Platform.runLater(extension::delete);
} }
}); });