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