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

Munich Clojure Workshop - 08 - Concurrency

Munich Clojure Workshop - 08 - Concurrency

Copyright © 2012-2014 Clojure Workshop Team: Maximilian Karasz, Jan Stępień, Marek Kubica, Oleksandr Petrov

Commercial use of complete work or parts of work is not allowed to anyone except for Clojure Workshop Team at whole or in parts.

Licensed under Creative Commons Attribution-NonCommercial 3.0: http://creativecommons.org/licenses/by-nc/3.0/

When using complete work or parts of work, explicit attribution is required.

Clojure Workshops

October 11, 2013
Tweet

More Decks by Clojure Workshops

Other Decks in Technology

Transcript

  1. Core Concepts • Concurrency • Parallelism • Shared State •

    Mutable Data Structures • Immutability • Shared Mutable State Friday, October 11, 13
  2. When a single thread won’t do (import '[java.util.concurrent Executors ExecutorService

    Callable]) (let [^ExecutorService pool (Executors/newFixedThreadPool 16) ^Callable callable (cast Callable (fn [] (reduce + (range 0 10000))))] (.submit pool callable)) Friday, October 11, 13
  3. $100 $40 $50 -60$ +10$ Consumer Consumer Consumer Consumer Identity

    Time Dereferencing Mutation Friday, October 11, 13
  4. (def a (atom {})) (swap! a assoc :b 1) a

    {} @reference value {:b 1} Friday, October 11, 13
  5. (def a (atom {})) (swap! a assoc :b 1) a

    {} @reference value {:b 1} Friday, October 11, 13
  6. (def a (atom {})) (swap! a assoc :b 1) (swap!

    a assoc :c 2) a @reference value {:b 1} {:b 1 :c 2} Friday, October 11, 13
  7. (def a (atom {})) (swap! a assoc :b 1) (swap!

    a assoc :c 2) a @reference value {:b 1} {:b 1 :c 2} Friday, October 11, 13
  8. (swap! my-atom <fn> <args>) Swaps current atom value by applying

    <fn> with <args> to it. Friday, October 11, 13
  9. STM • reference, not value • refs / agents /

    atoms / vars • transactional memory access yes, with retries • no user locks, no deadlocks • mutation through pure function • coordinated • readers can read value at any point in time Friday, October 11, 13
  10. STM • guarantees of “clear” values for readers and writers

    • atomic changes • changes are guaranteed • dereferenced values won’t change over the time Friday, October 11, 13
  11. STM • There are also Refs • synchronous, coordinated •

    manual transaction control • evaluated in `dosync` block • supports transactional mutations via `alter` • support commutative operations via `commute` Friday, October 11, 13
  12. STM • and Agents • uncoordianted, asynchronous updates • use

    their own thread pool • `send` for fixed thread pool • `send-off` for growing thread pool Friday, October 11, 13
  13. (def ft (future (+ 1 2 3 4 5 6)))

    ;; 㱺 #'user/ft ft ;; 㱺 #<core$future_call$reify__6110@effa25e: 21> @ft ;; 㱺 21 Friday, October 11, 13
  14. (def ft (future (reduce + (range 0 10000)))) ;; 㱺

    #'user/ft (realized? ft) ;; 㱺 true @ft ;; 㱺 49995000 Friday, October 11, 13
  15. Futures • Work with java.util.concurrent.Future • Perform job in background,

    return control immediately • Dereferencing/get blocks • Dereferencing is possible with timeout Friday, October 11, 13
  16. (let [l (java.util.ArrayList.)] (locking l (.add l 10)) l) ;;

    㱺 #<ArrayList [10]> Friday, October 11, 13
  17. Locking • Usually not required, because of immutable nature of

    DSs • May be needed in cases with heavy Java Interop Friday, October 11, 13
  18. (let [cnt (atom []) n 5 latch (java.util.concurrent.CountDownLatch. n)] (doseq

    [i (range 0 n)] (.start (Thread. (fn [] (swap! cnt conj i) (.countDown latch))))) (.await latch) @cnt) Friday, October 11, 13
  19. Latches • Very useful for tests • Useful for understanding

    parallel execution flows Friday, October 11, 13
  20. (let [l (AtomicLong.)] (dotimes [i 50] (.start (Thread. (fn []

    (.incrementAndGet l))))) (.get l)) ;; 㱺 49 Friday, October 11, 13
  21. Atomic Vars • Atomic values without arbitration • In majority

    of cases you can (should) use atoms Friday, October 11, 13