1

I have a strange behavior I cannot explain.

Please have a look at this minimal example:

public class ParallelStreamProgressMonitor
{
    public static void main(String[] args)
    {
        List<Integer> belege = IntStream.range(1, 100).boxed().collect(Collectors.toList());
        final ProgressMonitor pm = new ProgressMonitor(null, "Initialmessage", "Initial Note", 0, belege.size());
        pm.setMillisToDecideToPopup(0);
        pm.setMillisToPopup(0);
        pm.setMaximum(belege.size());
        pm.setNote("Now I am working");
        AtomicInteger counter = new AtomicInteger();
        belege.stream().parallel().forEach(b ->
        {
            System.out.println(b);
            pm.setProgress(counter.getAndIncrement());
            try
            {
                //something time consuming ...
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                // ignore
            }
        });
    }
}

When executing this, you'd normally expect that a ProgressMonitor will come up and show the progress of the execution.

But this is how it really looks like: Screenshot

It seems that for every parallel stream executions there is one ProgressMonitor instance extra showing up.

Is there a reason for this? How can I achieve that only one dialog shows up and shows the progress?

4

2 に答える 2

0

並列処理を使用する必要は本当にありますか? に置き換えない場合は belege.stream().parallel().forEach(b -> belege.stream().forEach(b ->問題が解決するはずです。

並列実行にはより多くのスレッドが含まれるため、進行状況モニターへの複数の呼び出しが異なるコンテキストから行われます。各スレッドは 1 つの進行状況 UI を表示し、最終的には複数の進行状況 UI を表示します。したがって、並列実行を使用する本当の理由がない場合は、順次実行を使用してください。

于 2020-12-18T10:46:29.613 に答える