Parallel programming was possible in Java only from Java 7 with the advent of
Join/Fork framework
.
Let's say in Java, using ExecutorService
I create a thread pool of say 4 threads and submit to it say 10 tasks which means 4 threads will start executing the 4 tasks and other 6 tasks will be picked up by threads as and when any thread finishes its task.
Let's assume I have a quad processor having 4 cores, I know a thread can be run on a single core(not taking hyper-threading concept here) so will all my 4 threads work in parallel, 1 thread running on 1 core? Isn't it a parallel programming?
EDIT: Source of reading - This is the Java 8 playlist where in chapter 1 it was referred that parallel programming was possible from Java 7 onwards.
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.
来源:https://stackoverflow.com/questions/44901843/parallel-programming-with-threads-in-java