GithubHelp home page GithubHelp logo

mtumilowicz / java11-spliterator-forkjoin Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 60 KB

Fork-join using spliterator and parallel streams.

Java 100.00%
fork-join spliterator parallel-computing parallel parallel-programming parallel-streams

java11-spliterator-forkjoin's Introduction

Build Status

java11-spliterator-forkjoin

Fork-join using spliterator and parallel streams.

Reference: https://java-8-tips.readthedocs.io/en/stable/parallelization.html

For more info about spliterators please refer my other github project:

Note that this project is the exact copy of my other github project: https://github.com/mtumilowicz/fork-join-find-minimum but using spliterator + parallel streams instead of fork-join API.

project description

Find minimum in the stream of ints in a parallel way with full control over splitting.

  1. We have int[] arr, so we will be using Spliterator.OfInt (it removes overhead of autoboxing).
  2. We implement Spliterator.OfInt in FindMinimumSpliterator in the same way like fork-join, but note that:
    • splitting tear away part of this (so this spliterator has to be mutable)
    • trySplit() returns
      • a Spliterator covering some portion of the elements
      • or null if this spliterator cannot be split
  3. Used characteristics:
    • IMMUTABLE - Characteristic value signifying that the element source cannot be structurally modified; that is, elements cannot be added, replaced, or removed, so such changes cannot occur during traversal.
    • NONNULL - Characteristic value signifying that the source guarantees that encountered elements will not be null.
    • ORDERED - Characteristic value signifying that an encounter order is defined for elements.
    • SIZED - Characteristic value signifying that the value returned from estimateSize() prior to traversal or splitting represents a finite size that, in the absence of structural source modification, represents an exact count of the number of elements that would be encountered by a complete traversal.
    • SUBSIZED - Characteristic value signifying that all Spliterators resulting from trySplit() will be both SIZED and SUBSIZED (This means that all child Spliterators, whether direct or indirect, will SIZED).
  4. Constructing parallel IntStream from Spliterator.OfInt:
    IntStream intStream = StreamSupport.intStream(new FindMinimumSpliterator(array, 0, array.length - 1), true);
    
  5. On the IntStream we found minimum calling min() method
    OptionalInt minimum = intStream.min();
    

tests

Tested in FindMinimumTest over array of 100_000 random elements.

java11-spliterator-forkjoin's People

Contributors

mtumilowicz avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.