所以,情況是這樣的:多線程對Java數組
//Some code...
public Map<String, String> someFunction() {
for (final UserDetail user : userDetailList) {
// the following (below) code runs in background thread
// long running task
new RecordPersonalisedDao().getPendingRecordsForUid(user.getuId(), new RecordPersonalisedDao.OnResultFetched() {
@Override
public void onResult(Result result) {
// callback after the user is processed
// put some parameter from result to map
map.put(user.getName(), result.getTotal());
}
});
}
// return map only after all users are processed
return map;
}
正如上面這段代碼的註釋中,我只希望用戶的整個列表進行處理後,要返回的最終地圖。
我不能更改RecordPersonalisedDao#getPendingRecordsForUid
的功能,以使其僅在主線程中運行。
如何在java中實現這一點?
編輯:這種類型的問題可以面對一般。所以,我想在java中理解相同的解決方案。
把我的問題,簡單地說,我要像
- 運行這段代碼在後臺陣列中的所有成員的行爲,一旦它的完成,發送一個回調。
(粗略地等)
[list_of_some_data]
.forEach(run this function)
.after(after the function is run in background for all members of list - return some value)
[用於Java線程安全的地圖(https://stackoverflow.com/questions/1792023/thread-safe-map-for-java) – Scrambo
使用join()方法的可能的複製等到後臺線程完成了。 –
你想要的是一個ExecutorService並使用通過提交一個任務作爲回調返回的Future。 – bhspencer