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?

2
  • Sure it can, why not? You're not synchronizing on list itself. Commented Sep 4, 2013 at 10:28
  • Yes It will...if you want avoid the same, then make syncronize on list...stackoverflow.com/questions/1431681/… might help you Commented Sep 4, 2013 at 11:22

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

So should i synchronize first the list and then the phone object or before removing the phone object i have to lock it?
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.
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;

Comments

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
}

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.