- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Parallel Data Processing in Java
The ‘Stream’ interface in Java, which was introduced in Java 8, is used to manipulate data collections in a declarative fashion. Stream interface can also be used to execute processes in parallel, without making the process too complicated. This means, a sequential stream can be declaratively turned into a parallel stream.
A parallel stream can be defined as a stream that splits the data collection elements into multiple streams. Every stream is assigned to a separate chunk and it is associated with a different thread. The work is divided between multiple threads, with the help of multiprocessors. This way, the CPU resources are efficiently used, and it is kept busy. A sequential stream can be converted into a parallel stream by prefixing the keyword ‘parallel’ to it. Following is an example −
Example
import java.util.stream.*; import java.util.Collections.*; public class Demo { static long sum_in_parallel(long n) { return Stream.iterate(1L, i->i + 1).limit(n).parallel().reduce(0L, Long::sum); } public static void main(String[] args) { long c = sum_in_parallel(23); System.out.println("Sum, when computed in parallel is " + c); } }
Output
Sum, when computed in parallel is 276
A class named Demo contains a function named ‘sum_in_parallel’ that takes a long integer as a parameter and returns the stream which can be iterated in parallel. In the main function, the ‘sum_in_parallel’ is called by passing a value, and this output is printed on the screen.
- Related Articles
- What is Parallel Processing?
- Parallel Distributed Processing Model
- Using parallel processing in SAP HANA
- What are the architecture of Parallel Processing?
- Scalable Data Processing in R
- In SAP ABAP, few records are skipped during parallel processing
- How does data mining relate to information processing and online analytical processing?
- Image processing/OpenCV image erosion Java Example.
- Image processing/OpenCV image dilation Java Example.
- DoubleStream parallel() method in Java
- IntStream parallel() method in Java
- LongStream parallel() method in Java
- Difference between Batch Processing and Stream Processing
- Image processing in Python?
- Bit-processing group in 8051
