A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/parallel-data-processing-java-set-1/ below:

Parallel Data Processing in Java | Set 1

Parallel Data Processing in Java | Set 1

Last Updated : 02 Nov, 2017

We know that new

Stream in Java

(introduced in Java 8) interface let us manipulate collections of data in a declarative way. In this topic, we will discover how the Stream interface gives us the opportunity to execute operations in parallel on a collection of data without much effort. It lets us declaratively turn a sequential stream into a parallel one

Definition and Making into Parallel Streams:

A parallel stream is one that splits the elements into multiple streams and assigns them into multiple chunks on different threads. Thus we can divide the workload of a given operation on the core of multiprocessors and thus it will make the CPU busy. We can convert the stream into parallel by attaching the keyword ‘parallel’. Following example just gives us the idea how we can convert a stream into a parallel one!

Java
// A Simple Java program to demonstrate parallel
// processing.
import java.util.stream.*;
import java.util.Collections.*;
public class JavaApplication1 {

    static long sumparallel(long n)
    {
        // Stream converted to parallel stream 
        return Stream.iterate(1L, i->i + 1).
                       limit(n).parallel(). 
                       reduce(0L, Long::sum);
    }

    // Driver code
    public static void main(String[] args)
    {
        long c = sumparallel(10);
        System.out.println("Sum is " + c);
    }
}

Output :

Sum is 55

In the next part, we will see the difference between the performance of parallel streams, sequential streams and iterative process and taking a review on certain more specialized methods in parallel streams.

Reference : https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4