wilkas wilkas - 9 months ago 28
Java Question

Writing to same ArrayList from several methods at the same time

I have several data listeners, which may receive datasets roughly at the same time. They will iterate datasets and store data to the same

ArrayList
by using its method
add()
. Could this potentially cause any issues of some data items not being stored?

Answer Source

You can use synchronized block in this case :

synchronized (this){
    // ...
}

You can find a good examples here Synchronized