How to make the collection classes thread-safe?

see the below example:
import java.util.*;
public class NameList {
private List names = Collections.synchronizedList(
new LinkedList());
public void add(String name) {
names.add(name);
}
public String removeFirst() {
if (names.size() > 0)
return (String) names.remove(0);
else
return null;
}}
The method Collections.synchronizedList() returns a List whose methods are all synchronized and "thread-safe" according to the documentation , like a Vector.
But the problem in above code is that does it check size() before removing any element.
What might happen here is that one of the threads will remove the one name
and print it, then the other will try to remove a name and get null.
The thing to realize here is that in a "thread-safe" class like the one returned by
synchronizedList(), each individual method is synchronized. So names.size()
is synchronized, and names.remove(0) is synchronized. But nothing prevents
another thread from doing something else to the list in between those two calls. And
that's where problems can happen.


The problem can be solved as below,
Without doing Collections.synchronizedList().
Instead, synchronize the code yourself:
import java.util.*;
public class NameList {
private List names = new LinkedList();
public synchronized void add(String name) {
names.add(name);
}
public synchronized String removeFirst() {
if (names.size() > 0)
return (String) names.remove(0);
else
return null;
}}
Now the entire removeFirst() method is synchronized, and once one thread
starts it and calls names.size(), there's no way the other thread can cut in and
steal the last name. The other thread will just have to wait until the first thread
completes the removeFirst() method.

No comments:

Post a Comment