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
  2. 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?
  3. 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
  4. 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
  5. “I don’t need thread-safe classes as I’m typically writing web

    apps without spawning any threads…” the average web dev
  6. 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
  7. 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?
  8. 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
  9. 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?
  10. 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
  11. 1

  12. 2

  13. 3

  14. 3+1

  15. 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
  16. 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
  17. Threading safely in Java P. Mihaylov Software Engineer, Tech. Trainer,

    Speaker [email protected] Java2Days 2020, A random zoom room on the Internet