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

Af78adc6caf494f74c1e4fb10a15c74a?s=128

Preslav Mihaylov

December 21, 2020
Tweet

Transcript

  1. Threading safely in Java P. Mihaylov Software Engineer, Tech. Trainer,

    Speaker preslav@pmihaylov.com Java2Days 2020, A random zoom room on the Internet
  2. y’all

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

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

  6. Why not a talk on concurrency?

  7. Most courses/books are incomplete… except one which you’ll learn about

    at the end of the talk...
  8. Not enough ❤ for thread-safety

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

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

    next steps
  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?
  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
  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
  14. What does “thread-safety” mean?

  15. Safe classes → behave correctly

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

    threads
  17. Should you care about it at all?

  18. “I don’t need thread-safe classes as I’m typically writing web

    apps without spawning any threads…” the average web dev
  19. You’re already writing multi-threaded applications

  20. None
  21. Thread-safety problems & solutions

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

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

  25. Shared mutable state

  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
  27. Atomic access to shared state

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

  30. None
  31. What can go wrong?

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

  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?
  35. Delegating thread-safety

  36. None
  37. None
  38. Delegated thread-safety → relying on the thread-safetiness of objects you

    use
  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
  40. Atomic compound actions

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

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

  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?
  46. Visibility & stale data

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

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

  51. None
  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
  53. Remember 3+1 things

  54. 1

  55. Stateless methods → thread-safe methods

  56. None
  57. 2

  58. Synchronize both reads and writes...

  59. None
  60. 3

  61. Prefer delegation over explicit synchronization

  62. None
  63. 3+1

  64. Be careful with compound actions...

  65. None
  66. None
  67. Is that all I need to know?

  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
  69. None
  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
  71. Get the mats → bit.ly/java2days2020-thread-safety

  72. Threading safely in Java P. Mihaylov Software Engineer, Tech. Trainer,

    Speaker preslav@pmihaylov.com Java2Days 2020, A random zoom room on the Internet