In my program there are 4 threads that addElemets to a model of a jlist at the same time. This causes jlist to blink, throw excpetions or even crash, because of the too many updates or repaints.
I tried to put some delay that fixed my problem but I was losing to much precious time.
What can I do?
this code simulates my problem:
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0; i<4; i++){
new Thread(new Runnable() {
@Override
public void run() {
for(int o=0; o<2000; o++){
model.addElement("add");
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}).start();
}
}
}).start();
The model.addElement(“add”); should be something like this:
This way you make sure that the elements are added under the EDT and not in some random thread.