4

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?

Joro Seksa
  • 1,525
  • 3
  • 18
  • 44
  • Sure it can, why not? You're not synchronizing on list itself. – Andrew Logvinov Sep 04 '13 at 10:28
  • Yes It will...if you want avoid the same, then make syncronize on list...http://stackoverflow.com/questions/1431681/correct-way-to-synchronize-arraylist-in-java might help you – Jayaram Sep 04 '13 at 11:22

6 Answers6

3

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.

Azodious
  • 13,752
  • 1
  • 36
  • 71
3

One operation has nothing to do with the other: synchronized doesn't protect access to any particular object. If your remove operation is within a synchronized block which uses the same object as the lock, then it will have to wait; if it isn't it will successfully remove the object, with no effect on the synchronized block which is using it as a lock. Removing the object from the list will not turn the object into garbage until its monitor is locked by any thread.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • So should i synchronize first the list and then the phone object or before removing the phone object i have to lock it? – Joro Seksa Sep 04 '13 at 10:29
  • This cannot be answered in general, but if you are just removing an item from the list and not changing anything on the item itself, I don't see why you would lock on the item at all. But most importantly, you must understand your own application logic on one side and the precise semantics of Java's locks on the other, and then combine those two into a locking scheme which gives you full thread safety. – Marko Topolnik Sep 04 '13 at 11:24
2

Yes.

Since you are synchronizing on the Phone instance, this is possible. Another thread can remove the same phone object from the ArrayList. But your phone reference will be pointing to the same instance.

To ensure that no other thread can access your list from somewhere else, synchronize on the list itself -

synchronized(phoneList)
{
    //some code runs here
}
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
2

Ofcourse. Locking Phone does not lock the List.

Also, to remove an element from the list you don't need to use the element. Removing is as simple as :

backingArray[i] = null;
rocketboy
  • 9,573
  • 2
  • 34
  • 36
2

Here, It just provides the synchronization for a group of statements inside synchronized and never cares about the phone instance obtained. Removing the phone object from the phoneList is possible. But remember Java does not delete an object from memory until it has some references left.

sr01853
  • 6,043
  • 1
  • 19
  • 39
1

When you do phoneList.get(index) it gives you the reference of the object from the list to Phone object.

Since only the phone object is synchronised you can remove elements from the list. Hence you will NOT get UnsupportedOperationException when you try to remove elements from the list.

But if the reference is lost or null. The you might get NullPointerException when you try to operate on the phone object.

AurA
  • 12,135
  • 7
  • 46
  • 63