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 Slide

  2. y’all

    View Slide

  3. View Slide

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

    View Slide

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

    View Slide

  6. Why not a talk on concurrency?

    View Slide

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

    View Slide

  8. Not enough ❤ for thread-safety

    View Slide

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

    View Slide

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

    View Slide

  11. 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 Slide

  12. 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 Slide

  13. 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 Slide

  14. What does “thread-safety” mean?

    View Slide

  15. Safe classes →
    behave correctly

    View Slide

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

    View Slide

  17. Should you care about it at all?

    View Slide

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

    View Slide

  19. You’re already writing
    multi-threaded applications

    View Slide

  20. View Slide

  21. Thread-safety problems & solutions

    View Slide

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

    View Slide

  23. View Slide

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

    View Slide

  25. Shared mutable state

    View Slide

  26. 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 Slide

  27. Atomic access to shared state

    View Slide

  28. View Slide

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

    View Slide

  30. View Slide

  31. What can go wrong?

    View Slide

  32. View Slide

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

    View 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 Slide

  35. Delegating thread-safety

    View Slide

  36. View Slide

  37. View Slide

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

    View Slide

  39. 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 Slide

  40. Atomic compound actions

    View Slide

  41. View Slide

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

    View Slide

  43. View Slide

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

    View Slide

  45. 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 Slide

  46. Visibility & stale data

    View Slide

  47. View Slide

  48. View Slide

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

    View Slide

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

    View Slide

  51. View Slide

  52. 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 Slide

  53. Remember 3+1 things

    View Slide

  54. 1

    View Slide

  55. Stateless methods →
    thread-safe methods

    View Slide

  56. View Slide

  57. 2

    View Slide

  58. Synchronize both reads and writes...

    View Slide

  59. View Slide

  60. 3

    View Slide

  61. Prefer delegation over explicit
    synchronization

    View Slide

  62. View Slide

  63. 3+1

    View Slide

  64. Be careful with compound actions...

    View Slide

  65. View Slide

  66. View Slide

  67. Is that all I need to know?

    View Slide

  68. 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 Slide

  69. View Slide

  70. 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 Slide

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

    View Slide

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

    View Slide