所以,情况是这样的:多线程对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