I want to ensure that the following class is thread-safe, should I use the synchronized keyword for other methods? Or use a thread-safe data structure to store Email. What should I do?
public class RecycleStation {
private static volatile RecycleStation uniqueInstance;
private static List<Email> recycleEmailList ;
private RecycleStation() {
recycleEmailList = new ArrayList<>();
}
public static RecycleStation getInstance() {
if (uniqueInstance == null) {
synchronized (RecycleStation.class) {
if (uniqueInstance == null) {
uniqueInstance = new RecycleStation();
}
}
}
return uniqueInstance;
}
public void RecycleEmail(Email email) {
recycleEmailList.add(email);
}
public void deleteEmail(Email email) {
recycleEmailList.remove(email);
}
public void clear() {
recycleEmailList.clear();
}
}
Aucun commentaire:
Enregistrer un commentaire