当前位置: 首页 > 知识库问答 >
问题:

使用ParallelStream-Java[duplicate]时抛出InterruptedException

司空浩邈
2023-03-14

我有一个嵌套for循环的方法,如下所示:

public MinSpecSetFamily getMinDomSpecSets() throws InterruptedException {
    MinSpecSetFamily result = new MinSpecSetFamily();
    ResourceType minRT = this.getFirstEssentialResourceType();
    if (minRT == null || minRT.noSpecies()) {
        System.out.println("There is something wrong with the "
                + "minimal rticator, such as adjacent to no species. ");
    }
    for (Species spec : minRT.specList) {
        ArrayList<SpecTreeNode> leafList = this.getMinimalConstSpecTreeRootedAt(spec).getLeaves();
        for (SpecTreeNode leaf : leafList) {
            result.addSpecSet(new SpecSet(leaf.getAncestors()));
        }
    }
    return result;
}
public MinSpecSetFamily getMinDomSpecSets() throws InterruptedException {
    ResourceType minRT = this.getFirstEssentialResourceType();
    if (minRT == null || minRT.noSpecies()) {
        System.out.println("There is something wrong with the "
                + "minimal rticator, such as adjacent to no species. ");
    }

    MinSpecSetFamily result = minRT.specList.parallelStream()
            .flatMap(spec -> getMinimalConstSpecTreeRootedAt(spec).getLeaves().parallelStream())
            .map(leaf -> new SpecSet(leaf.getAncestors()))
            .collect(MinSpecSetFamily::new, MinSpecSetFamily::addSpecSet, MinSpecSetFamily::addMSSF);
    return result;
}

在我想在'get leaves()'方法中引入interruptedexception之前,这一直很正常。现在,ParallelStream版本将不会编译,因为它说我有一个未报告的InterruptedException,必须捕获或声明要抛出该InterruptedException。我认为这是因为ParallelStream在多个线程上运行。IDE建议的try/catch块组合不能解决此问题。

在Interrupt parallel Stream execution中发布的第二个解决方案表明,我可能可以使用forkjoinpool解决这个问题,但我无法找到如何修改我的方法以使用这种方法。

共有1个答案

燕钟展
2023-03-14

如果您想要坚持您当前的设计,您只需要捕捉到异常:

.flatMap(spec -> {
     try {
       return getMinimalConstSpecTreeRootedAt(spec).getLeaves().parallelStream();
     } catch (InterruptedException e) {
       // return something else to indicate interruption
       // maybe an empty stream?
     }
 }).map(...)

注意,并行流的并行流可能是不必要的,并且仅对顶层流进行并行化可能在性能方面是足够的。

 类似资料:
  • 方法在什么条件下抛出?官方文件提到: NullPointerException-如果指定的集合包含null元素并且该集合不允许null元素,或者如果指定的集合为null 我如何确保这个“集合不允许空元素” 输出

  • 在我的spark代码中有gson,它存储在字符串RDD中,我将其转换为键和值。 (1). (2). 但我收到了以下错误消息。谷歌。格森。JsonSyntaxException:java。伊奥。EOFEException:com第1行第2列的输入结束。谷歌。格森。格森。com上的fromJson(Gson.java:813)。谷歌。格森。格森。com上的fromJson(Gson.java:768)

  • 问题内容: Visual Studio有一个选项,可以在引发未处理的异常时自动将其插入调试器,Eclipse是否具有类似的功能? 问题答案: 即使未捕获到这些异常,您也可以定义要在其上创建断点的确切的异常列表(应该等效于“ unhandled”)

  • 我使用的是Apache httpclient 4.3.6和httpcore 4.3.3(与opensaml 3.3.0捆绑)。我试图通过HTTPS通过代理获取网页,但每次都会收到SocketTimeoutException。HTTP连接工作正常。具体的超时并不重要;它只是需要更长的时间才能以更高的值失败。 示例代码: 尝试连接到代理时引发异常。堆栈跟踪是 这看起来很像Apache问题HTTPCLI

  • 我尝试使用这个命令< code>docker build -t myimage在django中运行docker。现在docker文件尝试运行< code > RUN pip install-r/app/requirements . txt-no-cache-dir ,但是当ot到达< code >下载psycopg2-2.9.3.tar.gz(380 kB)部分时,它会抛出错误。 注意:我的req

  • 问题内容: 以下哪一项正确?我希望编译器告诉我使用或 为什么我认为它不正确-> Bcoz NPE是RTE,但RTE不是NPE 这如何正确?我希望编译器告诉我使用or 或 回答: 对于RTE,即使您不向方法添加子句,编译器也不会说什么 但是,当我们明确地说“ ”时,为什么编译器会忽略它?与’ ;’ 相同 它不会在运行时抛出,例如某些对象被评估为null,并对该null对象调用了操作。通常,一个函数必