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

Threading Safely in Java

Threading Safely in Java

A talk on learning the fundamentals of thread-safety in Java as presented on the java2days conference (https://2020.java2days.com/).

Examples & follow-up:
http://bit.ly/java2days2020-thread-safety

Preslav Mihaylov

December 21, 2020
Tweet

More Decks by Preslav Mihaylov

Other Decks in Programming

Transcript

  1. Threading safely in Java
    P. Mihaylov
    Software Engineer, Tech. Trainer, Speaker
    [email protected]
    Java2Days 2020,
    A random zoom room on the Internet

    View full-size slide

  2. I like ‍ing, ing & (!)ing

    View full-size slide

  3. Check out my work @
    github.com/preslavmihaylov,
    pmihaylov.com

    View full-size slide

  4. Why not a talk on concurrency?

    View full-size slide

  5. Most courses/books are incomplete…
    except one which you’ll learn about at the end of the talk...

    View full-size slide

  6. Not enough ❤ for thread-safety

    View full-size slide

  7. Concurrency without thread-safety ->
    200 mp/h without safety measures

    View full-size slide

  8. Goals →
    1) Learn the fundamentals
    2) Get guidance on next steps

    View full-size slide

  9. What’s covered
    1. What does “thread-safety” mean?
    2. Should you care about it at all?
    3. Thread-safety problems & solutions
    a. When is there a potential thread-safety issue?
    b. Atomic access to shared state
    c. Delegating thread-safety
    d. Atomic compound actions
    e. Visibility & stale data
    4. Where to go from here?

    View full-size slide

  10. What’s not covered
    1. Initialisation safety, Immutable & effectively immutable objects
    2. Thread-confinement
    3. Exploring commonly used thread-safe collections in Java
    4. Managing threads with the Executors framework
    5. Extending thread-safe classes
    6. Managing thread lifecycle via cancellation/interrupts
    7. Dealing with performance/liveness issues (i.e. deadlock, livelock, etc.)
    8. Modern frameworks & libraries
    9. Advanced concurrency tooling - atomic variables/collections,
    synchronizers, explicit locks
    10. The Java Memory Model

    View full-size slide

  11. What’s not covered
    1. Initialisation safety, Immutable & effectively immutable objects
    2. Thread-confinement
    3. Exploring commonly used thread-safe collections in Java
    4. Managing threads with the Executors framework
    5. Extending thread-safe classes
    6. Managing thread lifecycle via cancellation/interrupts
    7. Dealing with performance/liveness issues (i.e. deadlock, livelock, etc.)
    8. Modern frameworks & libraries
    9. Advanced concurrency tooling - atomic variables/collections,
    synchronizers, explicit locks
    10. The Java Memory Model

    View full-size slide

  12. What does “thread-safety” mean?

    View full-size slide

  13. Safe classes →
    behave correctly

    View full-size slide

  14. Thread-safe classes →
    behave correctly
    while bombarded by
    a gazillion threads

    View full-size slide

  15. Should you care about it at all?

    View full-size slide

  16. “I don’t need
    thread-safe classes
    as I’m typically
    writing web apps
    without spawning
    any threads…”
    the average web dev

    View full-size slide

  17. You’re already writing
    multi-threaded applications

    View full-size slide

  18. Thread-safety problems & solutions

    View full-size slide

  19. When is there a potential
    thread-safety issue?

    View full-size slide

  20. Is it thread-safe?
    Join at slido.com #93849

    View full-size slide

  21. Shared mutable state

    View full-size slide

  22. Non-shared state
    Stack #1
    Thread #1
    Non-shared
    state #1
    Non-shared
    state #2
    Shared memory
    aka heap
    Shared state
    Non-shared state
    Stack #2
    Thread #2

    View full-size slide

  23. Atomic access to shared state

    View full-size slide

  24. Is it thread-safe?
    Join at slido.com #93849

    View full-size slide

  25. What can go wrong?

    View full-size slide

  26. synchronized is Java’s default way
    to add critical sections/locks

    View full-size slide

  27. Action point:
    1) Create an example app with the two versions of the class
    2) Start a dozen threads, invoking the methods concurrently
    3) What’s the value after the threads finish?

    View full-size slide

  28. Delegating thread-safety

    View full-size slide

  29. Delegated thread-safety →
    relying on the thread-safetiness of
    objects you use

    View full-size slide

  30. Action point:
    1) Create an example app with the two versions of the class
    2) Start a dozen threads, invoking the methods concurrently
    3) Prove that one of the classes is thread-safe, the other is not

    View full-size slide

  31. Atomic compound actions

    View full-size slide

  32. Is it thread-safe?
    Join at slido.com #93849

    View full-size slide

  33. Actions from the
    same invariant need to be
    synchronized together

    View full-size slide

  34. Action point:
    1) Create an example app with the two versions of the class
    2) Start a dozen threads, invoking the methods concurrently
    3) What’s the value after the threads finish?

    View full-size slide

  35. Visibility & stale data

    View full-size slide

  36. Is it thread-safe?
    Join at slido.com #93849

    View full-size slide

  37. Thread-safety is not only about
    concurrent writes...

    View full-size slide

  38. Action point:
    1) Create an example app with the previous example
    2) Start a dozen threads, invoking the methods concurrently
    3) Verify that the application hangs forever due to visibility issues

    View full-size slide

  39. Remember 3+1 things

    View full-size slide

  40. Stateless methods →
    thread-safe methods

    View full-size slide

  41. Synchronize both reads and writes...

    View full-size slide

  42. Prefer delegation over explicit
    synchronization

    View full-size slide

  43. Be careful with compound actions...

    View full-size slide

  44. Is that all I need to know?

    View full-size slide

  45. What’s not covered
    1. Initialisation safety, Immutable & effectively immutable objects
    2. Thread-confinement
    3. Exploring commonly used thread-safe collections in Java
    4. Managing threads with the Executors framework
    5. Extending thread-safe classes
    6. Managing thread lifecycle via cancellation/interrupts
    7. Dealing with performance/liveness issues (i.e. deadlock, livelock, etc.)
    8. Modern frameworks & libraries
    9. Advanced concurrency tooling - atomic variables/collections,
    synchronizers, explicit locks
    10. The Java Memory Model

    View full-size slide

  46. Action point:
    1) Buy the book
    2) Read every chapter at least twice. Focus on the first five ones
    3) Take book notes along the way
    a) OR use mine - github.com/preslavmihaylov/booknotes
    4) Finish the action points in this slide deck & continue doing that for
    all examples in the book

    View full-size slide

  47. Get the mats →
    bit.ly/java2days2020-thread-safety

    View full-size slide

  48. Threading safely in Java
    P. Mihaylov
    Software Engineer, Tech. Trainer, Speaker
    [email protected]
    Java2Days 2020,
    A random zoom room on the Internet

    View full-size slide