Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronized objects while iterating ArrayList

Let's say i have the following scenario:

final int index = 10;
Phone phone = phoneList.get(index);
synchronized(phone)
{
   //some code runs here
}

So while the the phone object (which is got throught phoneList.get() method) is locked can another thread execute the method:

phoneList.remove(index);

and null the phone object at the given index?

like image 253
Joro Seksa Avatar asked Dec 11 '22 11:12

Joro Seksa


1 Answers

Yes. why not?

But still, phone will be pointing to the same object. i.e. the object gets removed from list but the jvm still has reference to it.

like image 124
Azodious Avatar answered Jan 05 '23 04:01

Azodious