Parallel programming with threads in Java
Asked Answered
P

2

3

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.

Preciosa answered 4/7, 2017 at 9:15 Comment(3)
"I read that parallel programming was possible in Java only from Java 7 with the advent of Join/Fork framework." that is wrong. You could use threads since Java1.0. The framework just makes stuff easier to use.Durtschi
if you are not convinced with answers, probably you need to put your source where you read that. That would make easier for others to answer with full context.Sickert
Also note that you can find out the number of cores programmatically: #4760070 (sometimes people do +1 or -1)Tote
H
8

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.

Highpowered answered 4/7, 2017 at 9:18 Comment(3)
I have posted the link from where I got this wrong information. So just to be on same page, the example which I gave above of executing 10 tasks with 4 threads on 4-core processor is doing parallel programming. Right?Preciosa
Can you be a bit more specifc? first video, after minute 5, 10, 15, ?Highpowered
ForkJoinPool, part of the Fork/Join framework is an ExecutorService. And creating four thread to be executed on four cores is the best you can do trying to achieve the parallel execution, but there is never any guaranty that the threads will get executed that way…Mernamero
T
3

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.

enter image description here

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.

enter image description here

Teetotum answered 4/7, 2017 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.