Is it correct that the items that I added in my constructor to my volatile linked list below may not be visible to other threads
class ProductPrice {
private volatile LinkedList<QuantityPrice> quantityPriceList;
public ProductPrice(List<QuantityPrice> quantityPriceListParam) {
this.quantityPriceList = new LinkedList<QuantityPrice>();
quantityPriceList.addAll(quantityPriceListParam);
}
}
Would the following code where i assign the volatile variable after the list is loaded fix the issue. Because all happen before operations would also be visible.
private volatile LinkedList<QuantityPrice> quantityPriceList;
public ProductPrice(List<QuantityPrice> quantityPriceListParam) {
LinkedList<QuantityPrice> tempQuantityLinkedList = new LinkedList<QuantityPrice>();
tempQuantityLinkedList.addAll(quantityPriceListParam);
this.quantityPriceList = tempQuantityLinkedList;
}
and in this case could i just make the variable final and get the same effect of having all items visible to other threads.
private final LinkedList<QuantityPrice> quantityPriceList;
public ProductPrice(List<QuantityPrice> quantityPriceListParam) {
LinkedList<QuantityPrice> tempQuantityLinkedList = new LinkedList<QuantityPrice>();
tempQuantityLinkedList.addAll(quantityPriceListParam);
this.quantityPriceList = tempQuantityLinkedList;
}