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
Tweet

More Decks by Rodrigo Graciano

Other Decks in Technology

Transcript

  1. ➔ Software developer - Team lead ➔ Java Champion ➔

    Medellin Java Users Group Leader ➔ hibernate/hibernate-validator Contributor ➔ Duke’s Choice Award Winner ➔ Eclipse Collections contributor
  2. ➔ Professional with 10+ years ➔ Principal Software Engineer -

    NY ➔ NYJavaSIG Leader ➔ graciano.dev ➔ Twitter: @rodrigograciano
  3. • 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
  4. 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.
  5. • Synchronous: when we wait to finish a task before

    moving to the next one • Asynchronous: when we move to another task before it finishes
  6. • Thread t = new Thread( ()-> this.doSomething( thing ));

    t.start(); • 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();
  7. Stream API CompletableFuture Creation Chained operations Return forEach runAsync thenAccept

    / thenRun void map supplyAsync thenApply CompletableFuture<U>
  8. static ExecutorService newCachedThreadPool() static ExecutorService newFixedThreadPool (int nThreads) static ScheduledExecutorService

    newScheduledThreadPool (int corePoolSize) static ExecutorService newSingleThreadExecutor() static ScheduledExecutorService newSingleThreadScheduledExecutor() static ExecutorService newWorkStealingPool()
  9. T::get () throws InterruptedException , ExecutionException T::get (long timeout, TimeUnit

    unit) throws InterruptedException , ExecutionException , TimeoutException T::getNow (T valueIfAbsent) T::join()