Giter VIP home page Giter VIP logo

java12-stream-commonpool's Introduction

Build Status

java12-stream-commonPool

preface

  • https://github.com/mtumilowicz/fork-join-find-minimum
  • https://github.com/mtumilowicz/java11-spliterator-forkjoin
  • ForkJoinPool - ExecutorService for running ForkJoinTasks
    • is used by any ForkJoinTask that is not explicitly submitted to a specified pool
  • ForkJoinTask - is a thread-like entity that is much lighter weight than a normal thread
    • is a lightweight form of Future
    • computational task calculating pure functions or operating on purely isolated objects
    • primary coordination mechanisms are
      • fork - arranges to asynchronously execute this task in the pool the current task is running in, if applicable, or using the ForkJoinPool.commonPool() if not in ForkJoinPool
      • join - doesn't proceed until the task's result has been computed

project description

  • common ForkJoinPool supports parallel streams and CompletableFuture

  • common pool is common for the whole application, so there is possibility of saturation

  • common pool size vs available processors

    given:
    def processors = Runtime.getRuntime().availableProcessors()
    def poolSize = ForkJoinPool.commonPool().parallelism
    
    expect: 'differs at 1 thread, submitting thread'
    processors - 1 == poolSize
    
  • parallel - using fork-join pool

    given:
    def nums = 1..10
    
    when:
    def threads = nums.stream()
            .parallel()
            .map { extractThreads() }
            .collect Collectors.toSet()
    
    then: 'on my pc'
    Thread[ForkJoinPool.commonPool-worker-7,5,main]
    Thread[Test worker,5,main] // note that submitting thread interferes
    Thread[ForkJoinPool.commonPool-worker-13,5,main]
    Thread[ForkJoinPool.commonPool-worker-11,5,main]
    Thread[ForkJoinPool.commonPool-worker-9,5,main]
    Thread[ForkJoinPool.commonPool-worker-5,5,main]
    Thread[ForkJoinPool.commonPool-worker-15,5,main]
    Thread[ForkJoinPool.commonPool-worker-3,5,main]
    
  • drawbacks of mixing threads from a pool with a submitting thread:

    • http://coopsoft.com/ar/Calamity2Article.html#submit
    • submitting thread’s stack is contaminated with work that should be independent of it
    • this practice violates a fundamental principle of good programming in not separating a caller from the external processing
  • -Djava.util.concurrent.ForkJoinPool.common.parallelism=100

  • we could run stream on a dedicated thread pool

    given:
    def nums = 1..10
    
    and:
    def stream = nums.stream()
            .parallel()
            .map { printThreads() }
    
    when:
    process(stream) // it doesn't really matter where you create the stream but where you invoke terminal operation
    
    then: 'on my pc'
    Thread[ForkJoinPool-1-worker-101,5,main]
    Thread[ForkJoinPool-1-worker-17,5,main]
    Thread[ForkJoinPool-1-worker-3,5,main]
    Thread[ForkJoinPool-1-worker-59,5,main]
    Thread[ForkJoinPool-1-worker-73,5,main]
    Thread[ForkJoinPool-1-worker-45,5,main]
    Thread[ForkJoinPool-1-worker-75,5,main]
    Thread[ForkJoinPool-1-worker-87,5,main]
    Thread[ForkJoinPool-1-worker-89,5,main]
    Thread[ForkJoinPool-1-worker-117,5,main]
    

    where:

    static void process(stream) throws InterruptedException {
        def pool = new ForkJoinPool(50)
        pool.submit { stream.forEach {} }
        pool.shutdown()
        pool.awaitTermination(30, TimeUnit.SECONDS)
    }
    

    Note, however, that this technique of submitting a task to a fork-join pool to run the parallel stream in that pool is an implementation "trick" and is not guaranteed to work. Indeed, the threads or thread pool that is used for execution of parallel streams is unspecified. By default, the common fork-join pool is used, but in different environments, different thread pools might end up being used. (Consider a container within an application server.)

    Stuart Marks

java12-stream-commonpool's People

Contributors

mtumilowicz 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.