繁体   English   中英

为什么流不能在并行模式下工作?

[英]Why the stream doesn't work in parallel mode?

我有一个代码:

rows = types.stream().parallel().map(s -> {
    switch (s){
        case NDBaseDocumentModel.DOC_TYPE_NSS:
            return getDocsOfSpecificTypeAndCountItsDupes(NDGostStandardsModel.TYPE_ND_GOST_STANDARDS, null,systemMessages.getString("form20.nss.name")).values();
        case NDBaseDocumentModel.DOC_INT_REG_STANDARDS:
            return getDocsOfSpecificTypeAndCountItsDupes(NDIntRegStandardsModel.TYPE_ND_INT_REG_STANDARDS, null,systemMessages.getString("form20.int_reg")).values();
    }
    return new HashSet<Form20Row>();
}).flatMap(o -> {
    return o.stream();
}).collect(Collectors.toList());

它不起作用(返回零行),但是当我删除“parallel()”调用(相同数据上的 14 行)时工作正常。 有人能告诉我为什么吗?? 请?

并行流使用线程池进行处理,因此您的方法getDocsOfSpecificTypeAndCountItsDupes需要是线程安全的。

由于删除parallel()并在同一线程上处理它可以解决您的问题,因此该方法中似乎有一些不是线程安全的东西,这将是您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM