That's not going to work I'm affraid. I'm guessing they may have been looking for an answer along the lines of retrieving a List instance via Collections.synchronizedList() and taking the measures detailed in the API documentation of that method.
Edit: Ugh, too slow. [ September 13, 2008: Message edited by: Jelle Klap ]
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
posted 10 years ago
Ok. Sadly , i did not know about it. But good point ..... I got selected.
In your solution, only part of the method call is synchronized. The list remains the same, without any synchronization of its own.
Collections.synchronizedList handles that internally. It returns a new List wrapper that is implemented simlarly like this:
Now it uses a specified object as a mutex because of synchronizedMap (it's keySet(), values() and entrySet() methods return different collections that need the same mutex), but for List it's actually the same as this: