有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java最终对象引用是否保证了所有线程之间的可见性?

我在读这篇文章:

https://www.javamex.com/tutorials/synchronization_final.shtml

它说:

The fields on any object accessed via a final reference are also guaranteed to be at least as up to date as when the constructor exits. This means that: Values of final fields, including objects inside collections referred to by a final reference, can be safely read without synchronization.

假设我的课程如下:

public class Cache {


private Map<String, Currency> currencyMap = new ConcurrentHashMap<String, Currency>();
private List<Currency> currencyList = new ArrayList<Currency>();
}

如果我在我的主应用程序类中声明:

private final Cache cache;

这是否意味着如果线程A更新了currencyMap和currencyList,那么这将保证线程B在currencyMap和currencyList中看到最新的条目


共 (0) 个答案