調用this.stations = targetGet.request(...)
只能修改類StationsCall
領域stations
,它不會修改實際的單身人士,你應該甚至無法創建一個Stations
的實例,因爲它的構造函數是private
。
你需要的是一個設置器在你的單身,以設置當前的電臺列表。
事情是這樣的:
public class Stations {
// Use an AtomicReference to make sure that all threads see the last list of stations
private final AtomicReference<List<Station>> stations = new AtomicReference<>();
private Stations() {
// Exists only to defeat instantiation.
}
public static Stations getInstance() {
// Lazy create your instance of Stations using a static inner class
// for thread safety
return StationsHolder.INSTANCE;
}
public List<Station> getStations(){
// Get the last list of stations from the AtomicReference
return this.stations.get();
}
public void setStations(List<Station> stations) {
// Set the new list of stations and make it unmodifiable for thread safety
this.stations.set(Collections.unmodifiableList(stations));
}
private static class StationsHolder {
private static final Stations INSTANCE = new Stations();
}
}
注:我提高你的代碼,使其線程安全的,一個單必須通過併發線程使用好機會。
然後你StationsCall
類將是:
public void execute(JobExecutionContext context) throws JobExecutionException {
...
Stations.getInstance().setStations(targetGet.request(...));
}
public List<Station> getStations(){
return Stations.getInstance().getStations();
}
假設你真正需要的是讓站的當前列表只集中其訪問的能力,你不如果不在乎是單身或沒有,那麼你的代碼,而應該是:
public class Stations {
// Use an AtomicReference to make sure that all threads see the last instance
private static final AtomicReference<Stations> INSTANCE =
new AtomicReference<>(new Stations());
private List<Station> stations;
public Stations() {
}
public Stations(final List<Station> stations) {
// Make a safe and unmodifiable copy of the list of stations
this.stations = Collections.unmodifiableList(new ArrayList<>(stations));
}
public static Stations getInstance() {
return INSTANCE.get();
}
public List<Station> getStations(){
return this.stations;
}
public static void setInstance(Stations stations) {
// Set the new instance
INSTANCE.set(new Stations(stations.getStations()));
}
}
然後你StationsCall
類將是:
public void execute(JobExecutionContext context) throws JobExecutionException {
...
Stations.setInstance(targetGet.request(...));
}
public List<Station> getStations(){
return Stations.getInstance().getStations();
}
您正在使用'私人車站車站=新車站();'這裏。這創建了一個實例,但與單例實例不同。使用'Stations.getInstance()'代替 – AxelH
對不起,我現在編輯的代碼,它不工作。 – Fr33d0m
'this.stations = targetGet.request ...'會收到單例引用?似乎你每次重新創建一個新的「Stations」,這是奇怪的有一個私人構造... – AxelH