I know this is a daft questions but cant work out how to fix this, I have not had much experience with using threads before.
Below should create first of all a timer which will execute the command output.write(mylist) every 10 seconds which will simply output the contents of mylist.
Secondly it loops through about 3 lists I have and for each them creates a thread which will continue to loop through getting the next word in the list. Please note: this is stripped down version and not complete so please don't comment on the arraylist / list but rather the error itself.
There is a concurrent modification exception happening frequently but not all the time when it tries to do output.write()
. I am guessing this is because one of the other threads are currently saving something to mylist
? How would I go about fixing this?
Runnable timer = new Runnable() {
public void run() {
try {
while (true) {
Thread.sleep(10000);
output.write(mylist);
}
} catch (InterruptedException iex) {}
}
};
Thread timerThread = new Thread(timer);
timerThread.start();
for (final ValueList item : list) {
Runnable r = new Runnable() {
public void run() {
try {
while (true) {
char curr = item.getNext();
mylist.addWord(curr);
}
} catch (InterruptedException iex) {}
}
};
Thread thr = new Thread(r);
thr.start();
}
}