Invoke vs Get ForkJoinPool

huangapple go评论49阅读模式
英文:

Invoke vs Get ForkJoinPool

问题

将代码部分翻译如下:

import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

public class TestSync {

	public static void main(String[] args) {
		// 创建一个整数列表
		List<Integer> intArray = IntStream.rangeClosed(1, 500).boxed().toList();

		System.out.println("元素数量: " + intArray.size());
		
		AtomicInteger i = new AtomicInteger(intArray.size());
		// 创建一个自定义线程池
		ForkJoinPool customThreadPool = new ForkJoinPool(50);
		
		// 使用自定义线程池提交任务
		customThreadPool.submit(() -> {
			// 并行处理整数列表中的元素
			intArray.parallelStream().forEach(sp -> {
				System.out.println("剩余整数: " + i.decrementAndGet());
			});
		}).invoke();  // 使用invoke()方法执行任务
		customThreadPool.shutdown();
	}

}

相关的输出是:

元素数量: 500
剩余整数: 499
剩余整数: 498
剩余整数: 497
剩余整数: 495
剩余整数: 494
剩余整数: 493
剩余整数: 492
剩余整数: 491
剩余整数: 490
剩余整数: 489
剩余整数: 488
剩余整数: 487
....
剩余整数: -74
剩余整数: -75
剩余整数: -66
剩余整数: -77
剩余整数: -78
剩余整数: -79
剩余整数: -80
剩余整数: -81
剩余整数: -82
剩余整数: -83
...

如果我用get()替换invoke(),负数将不会出现。

请问有人能帮我理解invoke()get()之间的区别吗?

英文:

Premise: I'm not very familiar with concurrency.

Take a look at this code:

import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

public class TestSync {

	public static void main(String[] args) {
		List&lt;Integer&gt; intArray = IntStream.rangeClosed(1, 500).boxed().toList();

		System.out.println(&quot;Element size: &quot; + intArray.size());
		
		AtomicInteger i = new AtomicInteger(intArray.size());
		ForkJoinPool customThreadPool = new ForkJoinPool(50);
		
		customThreadPool.submit( () -&gt; {
			intArray.parallelStream().forEach(sp -&gt; {
				System.out.println(&quot;Remaining int: &quot; + i.decrementAndGet());
			});
		}).invoke();
		customThreadPool.shutdown();
	}

}

The related output is:

Element size: 500
Remaining int: 499
Remaining int: 498
Remaining int: 497
Remaining int: 495
Remaining int: 494
Remaining int: 493
Remaining int: 492
Remaining int: 491
Remaining int: 490
Remaining int: 489
Remaining int: 488
Remaining int: 487
....
Remaining int: -74
Remaining int: -75
Remaining int: -66
Remaining int: -77
Remaining int: -78
Remaining int: -79
Remaining int: -80
Remaining int: -81
Remaining int: -82
Remaining int: -83
...

If I replace the invoke() with get(), negative numbers will never appear.

Can some one help me to understand the difference between invoke() and get()?

答案1

得分: 1

当你在invoke()上执行ForkJoinTask时,你强制它执行。
但在此之前,你已经将它提交到池中执行,所以你执行了两次。

执行get()会等待任务(已经提交到池中)完成并返回结果。

英文:

When you do invoke() on the ForkJoinTask you force it to execute.
But before you already submitted it for execution in the pool, so you execute it twice.

Performing get() will wait for the task (that was already submitted to the pool) to complete and return the result.

huangapple
  • 本文由 发表于 2023年3月7日 21:46:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定