Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Of concurrency and other demons

Of concurrency and other demons

Talk about concurrency.
Initially presented at DawsCon with Hillmer Chona

Rodrigo Graciano

January 16, 2021

More Decks by Rodrigo Graciano

Other Decks in Technology


  1. ➔ Software developer - Team lead
    ➔ Java Champion
    ➔ Medellin Java Users Group Leader
    ➔ hibernate/hibernate-validator Contributor
    ➔ Duke’s Choice Award Winner
    ➔ Eclipse Collections contributor

    View full-size slide

  2. ➔ Professional with 10+ years
    ➔ Principal Software Engineer - NY
    ➔ NYJavaSIG Leader
    ➔ graciano.dev
    ➔ Twitter: @rodrigograciano

    View full-size slide

  3. View full-size slide

  4. ● Concurrency when we have many task, largely independent, and we use the same
    resource to do them at a time
    ● Parallelism is when we want to do one task, and we split up in multiple subtasks

    View full-size slide

  5. A Thread is an independent path of execution that allows a program to operate more efficiently by doing
    multiple things at the same time.
    Threads can be used to perform complicated tasks in the background without interrupting the main program.

    View full-size slide

  6. ● Synchronous: when we wait to finish a task before moving to the next one
    ● Asynchronous: when we move to another task before it finishes

    View full-size slide

  7. Thread t = new Thread( ()-> this.doSomething( thing ));

    ExecutorService executor = Executors.newFixedThreadPool( 10 );
    Future> future = executor.submit( ()-> this.doSomething( thing ));

    CompletableFuture.runAsync( ()-> this.doSomething( thing ));

    Thread.builder().virtual().name("name").task(() -> this.doSomething( thing )).start();

    View full-size slide

  8. Stream API CompletableFuture
    Creation Chained operations Return
    forEach runAsync thenAccept / thenRun void
    map supplyAsync thenApply CompletableFuture

    View full-size slide

  9. CompletableFuture::runAsync (Runnable runnable)
    CompletableFuture::supplyAsync (Supplier supplier)
    CompletableFuture::runAsync (Runnable runnable, Executor executor)
    CompletableFuture::supplyAsync (Supplier supplier, Executor executor)

    View full-size slide

  10. static ExecutorService newCachedThreadPool()
    static ExecutorService newFixedThreadPool (int nThreads)
    static ScheduledExecutorService newScheduledThreadPool (int corePoolSize)
    static ExecutorService newSingleThreadExecutor()
    static ScheduledExecutorService newSingleThreadScheduledExecutor()
    static ExecutorService newWorkStealingPool()

    View full-size slide

  11. T::get () throws InterruptedException , ExecutionException
    T::get (long timeout, TimeUnit unit) throws InterruptedException ,
    ExecutionException , TimeoutException
    T::getNow (T valueIfAbsent)

    View full-size slide

  12. View full-size slide