I have a 2nd Thread that i use to send messages using OSC. From the main Thread i add messages, I had a problem with ConcurrentModificationException.
What I did to fix it is I made a new List with messages to add. In the 2nd Thread i add those messages to the list i want to send.
At the moment it runs without any problems, but i'm wondering is that luck? In other words, could it be that the change to run into a ConcurrentModificationException is still there but is really small now or did i really fix the problem?
public void run() {
while (running) {
toSend.addAll(newMessages);
newMessages.clear();
Iterator<OSCPriorityMessage> itr = toSend.iterator();
while (itr.hasNext()) {
OSCPriorityMessage msg = itr.next();
oscP5.send(msg, netAddress);
itr.remove();
}
try {
sleep((long)(waitTime));
}
catch (Exception e) {
}
}
}
Here i add the messages to send:
public void send(OSCPriorityMessage msg) {
newMessages.add(msg);
}