Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

y’all

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

I like ‍ing, ing & (!)ing

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Why not a talk on concurrency?

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Not enough ❤ for thread-safety

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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?

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

What does “thread-safety” mean?

Slide 15

Slide 15 text

Safe classes → behave correctly

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Should you care about it at all?

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

You’re already writing multi-threaded applications

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Thread-safety problems & solutions

Slide 22

Slide 22 text

When is there a potential thread-safety issue?

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Shared mutable state

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Atomic access to shared state

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

What can go wrong?

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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?

Slide 35

Slide 35 text

Delegating thread-safety

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Atomic compound actions

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Actions from the same invariant need to be synchronized together

Slide 45

Slide 45 text

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?

Slide 46

Slide 46 text

Visibility & stale data

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

Remember 3+1 things

Slide 54

Slide 54 text

1

Slide 55

Slide 55 text

Stateless methods → thread-safe methods

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

2

Slide 58

Slide 58 text

Synchronize both reads and writes...

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

3

Slide 61

Slide 61 text

Prefer delegation over explicit synchronization

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

3+1

Slide 64

Slide 64 text

Be careful with compound actions...

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Is that all I need to know?

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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