Javatpoint Logo
Javatpoint Logo

Java ForkJoinPool invoke() Method

The invoke() method of ForkJoinPool class performs the task specified as the parameter and returns the result on its completion. If the computation encounters an unchecked Exception or Error, it is thrown as the outcome of this invocation.

Syntax

Parameter

T - The type of the task's result

Returns

Task - the task

Throws

NullPointerException - if the task is null

RejectedExecutionException - if the task cannot be scheduled for execution

Example 1

Test it Now

Output:

Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 0 to 2
Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 2 to 5
Thread[ForkJoinPool-3-worker-1,5,main] computing: 0 to 2
Thread[ForkJoinPool-2-worker-1,5,main] computing: 0 to 2
Thread[ForkJoinPool-1-worker-1,5,main] computing: 0 to 2
Thread[ForkJoinPool-1-worker-1,5,main] computing: 2 to 5
Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 5 to 7
Thread[ForkJoinPool.commonPool-worker-1,5,main] computing: 7 to 10
87

Example 2

Test it Now

Output:

Common Pool Size before  :0
Common Pool Size after  :3
Next TopicJava ForkJoinPool




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials