我有一个637200的rollids列表,我用它来循环并从dynamodb表中获取它们并尝试将它存储在hashmap中。我试图将此添加到大约8 ForkJoinPool
的自定义Threads
HashMap<String, List<Details>> studentRecord = new HashMap<>();
rollIds.parallelStream(id -> {
Details d = fetchDataStudentDao.getStudentById(id);
studentRecord.put(id, d);
});
注意:我无法控制getStudentById
,因为它来自其他一些我不应该改变的包。
我在使用并行流时遇到问题,因为映射没有正确发生。有一个id
的学生被映射到其他一些学生的详细信息。
在检查更多内容时,我发现HashMap
不是线程安全的,不应该使用,而是使用concurrentHashMap
或者使用forEach。
如果我使用forEach,则需要4个多小时才能得到结果。但如果我使用parallelStreams,它比forEach稍微快3个小时,但结果不正确。
我面临的问题,我应该使用哪种解决方案?我不知道如何在这种情况下使用ConcurrentHashmap
并且会更快吗?
或者任何其他方式,它可以更快,一些帮助和指导非常需要。
你的put(id,d)只放一个细节,而不是一个细节列表。
请尝试使用此代码
Map<String, Details> studentRecords = rollIds.parallelStream()
.collect(Collectors.toMap(Functions.identity(), id -> fetchDataStudentDao.getStudentById(id)));