Does Collection.SynchronizedList iterator block entire iteration process or just .next() part?

80 Views Asked by At

Before I ask anything you might tell me to read docs. But I am still a beginner and I am not handling docs so easily. My question is does iterator in SynchronizedList block 'entire iteration process (loop)' or just its methods?

Meaning: Can another thread do some work on synchronizedList instance while iteration is 'going on'? Or it can do some work after thread that called next(), as I noticed next() is also synchronized.

I would appreciate any help, thanks :)

1

There are 1 best solutions below

6
On

does iterator in SynchronizedList block 'entire iteration process (loop)' or just its methods?

Just its methods.

There is no way for the class to detect if it is being used in a loop.

If you need the list to be synchronised for the entirety of a loop, externally synchronize on the list:

synchronized (list) {
  for (E element : list) { ... }
}