source:
public static void invokeAll(ForkJoinTask<?> t1, ForkJoinTask<?> t2) {
t2.fork();
t1.invoke();
t2.join();
}
why not use :
public static void invokeAll(ForkJoinTask<?> t1, ForkJoinTask<?> t2) {
t1.fork();
t2.fork();
t1.join();
t2.join();
}
According to the documentation for ForkJoinTask
:
Method
invoke()
is semantically equivalent tofork(); join()
fork(); join()
but always attempts to begin execution in the current thread.
So the version with invoke()
is doing the same thing as your version, but instead of passing both jobs off to two different threads, it's passing t2
off to another thread and trying to handle t1
itself. That's less passing around, and less threads involved.
In your version, the current thread has nothing to do but wait once it fork()
s both tasks.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.