Parallel programming with threads in Java

我与影子孤独终老i 提交于 2019-12-04 13:38:35

There is a misconception on your end.

I read that parallel programming was possible in Java only from Java 7 with the advent of Join/Fork framework.

That is wrong. The Join/Fork statement is simply yet another abstraction layer that gives you more powerful concepts to work with - compared to "bare metal" threads.

Isn't it a parallel programming?

You have outlined clearly that your tasks will go into a pool that supports 4 threads; and that your hardware should support 4 threads as well. So that work will happen in parallel. And please not: Fork/Join is not the same as ExecutorService. Instead, both are advanced concepts; intended to make "parallel programming" easier for you.

After briefly listening into the video linked in the question: the tutorial is about the fact that Java8 added streams, and on top of that parallel streams (which use the Fork/Join framework underneath - that one was introduced with Java 7).

In any case, that video emphasizes that parallel program gets much much simpler compared to earlier versions of Java. So it is not at all about introducing something that wasn't possible before - but about providing new more powerful abstractions that make it easier to do such things.

Parallel programming was possible in Java only from Java 7 with the advent of Join/Fork framework.

Parallel programming exists in java since early versions. It was enhanced with Java 5 java.util.concurrent package classes and Java 7 ForkJoinPool further enhanced parallel programming to new level.

You can find answer to your questions in this article by oracle.

Java Platform, Standard Edition (Java SE) 5 and then Java SE 6 introduced a set of packages providing powerful concurrency building blocks. Java SE 7 further enhanced them by adding support for parallelism

Example of divide and conquer problem:

Find sum of integers in large array

Instead of sequentially computing the sum, divide array into multiple partitions and assign computation task on each partition to different task.

The problem with the executors for implementing divide and conquer algorithms is not related to creating subtasks, because a Callable is free to submit a new subtask to its executor and wait for its result in a synchronous or asynchronous fashion.

The issue is that of parallelism: When a Callable waits for the result of another Callable, it is put in a waiting state, thus wasting an opportunity to handle another Callable queued for execution.

The fork/join framework added to the java.util.concurrent package in Java SE 7

Additions for Supporting Parallelism:

The core addition is a new ForkJoinPool executor that is dedicated to running instances implementing ForkJoinTask. ForkJoinTask objects support the creation of subtasks plus waiting for the subtasks to complete. With those clear semantics, the executor is able to dispatch tasks among its internal threads pool by “stealing” jobs when a task is waiting for another task to complete and there are pending tasks to be run.

ForkJoinTask objects feature two specific methods:

The fork() method allows a ForkJoinTask to be planned for asynchronous execution. This allows a new ForkJoinTask to be launched from an existing one.

In turn, the join() method allows a ForkJoinTask to wait for the completion of another one.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!