Slide 1

Slide 1 text

THE MUTABLE STATE MONSTER AND HOW TO DEFEAT IT ANUP COWKUR

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

PROLOGUE

Slide 4

Slide 4 text

WHAT IS STATE?

Slide 5

Slide 5 text

WHAT IS STATE? WIKIPEDIA A computer program stores data in variables, which represent storage locations in the computer's memory. The contents of these memory locations, at any given point in the program's execution, is called the program's state.

Slide 6

Slide 6 text

STATE IS THE CURRENT VALUE OF DATA AT ANY POINT OF EXECUTION OF THE PROGRAM –Me TEXT

Slide 7

Slide 7 text

CHAPTER 1: THE WIZARD

Slide 8

Slide 8 text

A LONG TIME AGO, THERE WAS A GRAND WIZARD

Slide 9

Slide 9 text

JOHN MCCARTHY ACTUAL PHOTO

Slide 10

Slide 10 text

HE PIONEERED ARTIFICIAL INTELLIGENCE

Slide 11

Slide 11 text

IN 1958, HE CAME UP WITH A LITTLE LANGUAGE CALLED LISP

Slide 12

Slide 12 text

LISP ACTUAL SYNTAX

Slide 13

Slide 13 text

LISP WAS THE FIRST LANGUAGE TO HAVE AUTOMATIC MEMORY MANAGEMENT USING A GARBAGE COLLECTOR

Slide 14

Slide 14 text

IT WAS ALSO ONE OF THE FIRST LANGUAGE TO HAVE ATOMS - DATA THAT COULD NOT BE DIVIDED FURTHER

Slide 15

Slide 15 text

BUT WHY?

Slide 16

Slide 16 text

CHAPTER 2: THE MONSTER

Slide 17

Slide 17 text

DO THE FOLLOWING PROGRAMS WORK CORRECTLY IN A MULTI- THREADED SYSTEM?

Slide 18

Slide 18 text

PROGRAM 1

Slide 19

Slide 19 text

public void checkAndPut(final String key,
 final String value) {
 if (!map.containsKey(key)) {
 map.put(key, value);
 }
 }

Slide 20

Slide 20 text

public void checkAndPut(final String key,
 final String value) {
 if (!map.containsKey(key)) {
 map.put(key, value);
 }
 }

Slide 21

Slide 21 text

public void checkAndPut(final String key,
 final String value) {
 if (!map.containsKey(key)) {
 map.put(key, value);
 }
 }

Slide 22

Slide 22 text

public void checkAndPut(final String key,
 final String value) {
 if (!map.containsKey(key)) { < - Thread 1
 map.put(key, value);
 }
 }

Slide 23

Slide 23 text

public void checkAndPut(final String key,
 final String value) {
 if (!map.containsKey(key)) {
 map.put(key, value);
 }
 } Thread 2 removes key from map

Slide 24

Slide 24 text

public void checkAndPut(final String key,
 final String value) {
 if (!map.containsKey(key)) {
 map.put(key, value); <- Thread 1
 }
 }

Slide 25

Slide 25 text

public void checkAndPut(final String key,
 final String value) {
 if (!map.containsKey(key)) {
 map.put(key, value);
 }
 }

Slide 26

Slide 26 text

CHECK-THEN-ACT PROBLEM

Slide 27

Slide 27 text

WE CAN FIX IT!

Slide 28

Slide 28 text

public void checkAndPut(final String key,
 final String value) {
 synchronized (this) {
 if (!map.containsKey(key)) {
 map.put(key, value);
 }
 }
 }

Slide 29

Slide 29 text

public void checkAndPut(final String key,
 final String value) {
 synchronized (this) {
 if (!map.containsKey(key)) {
 map.put(key, value);
 }
 }
 }

Slide 30

Slide 30 text

PROGRAM 2

Slide 31

Slide 31 text

public class EntCounter {
 private long count = 0;
 
 public long increment() {
 return ++count;
 }
 }

Slide 32

Slide 32 text

public class EntCounter {
 private long count = 0;
 
 public long increment() {
 return ++count;
 }
 }

Slide 33

Slide 33 text

public class EntCounter {
 private long count = 0;
 
 public long increment() {
 return ++count;
 }
 }

Slide 34

Slide 34 text

public class EntCounter {
 private long count = 0;
 
 public long increment() {
 return ++count; <- Thread 1
 }
 }

Slide 35

Slide 35 text

public class EntCounter {
 private long count = 0;
 
 public long increment() {
 1. Read
 return ++count; 2. Modify
 3. Write 
 }
 }

Slide 36

Slide 36 text

public class EntCounter {
 private long count = 0;
 
 public long increment() {
 1. Read <- Thread 1
 return ++count; 2. Modify
 3. Write 
 }
 }

Slide 37

Slide 37 text

public class EntCounter {
 private long count = 0;
 
 public long increment() {
 1. Read <- Thread 1
 return ++count; 2. Modify
 3. Write 
 }
 } Thread 2 modifies the value of count

Slide 38

Slide 38 text

public class EntCounter {
 private long count = 0;
 
 public long increment() {
 1. Read
 return ++count; 2. Modify <- Thread 1
 3. Write 
 }
 }

Slide 39

Slide 39 text

public class EntCounter {
 private long count = 0;
 
 public long increment() {
 1. Read
 return ++count; 2. Modify
 3. Write <- Thread 1 
 }
 }

Slide 40

Slide 40 text

public class EntCounter {
 private long count = 0;
 
 public long increment() {
 1. Read
 return ++count; 2. Modify
 3. Write
 }
 }

Slide 41

Slide 41 text

READ-MODIFY- WRITE PROBLEM

Slide 42

Slide 42 text

BUT WE CAN FIX IT!

Slide 43

Slide 43 text

public class EntCounter {
 private long count = 0;
 
 public long increment() {
 synchronized (this) {
 return ++count;
 }
 }
 }

Slide 44

Slide 44 text

public class EntCounter {
 private long count = 0;
 
 public long increment() {
 synchronized (this) {
 return ++count;
 }
 }
 }

Slide 45

Slide 45 text

PROGRAM 3

Slide 46

Slide 46 text

public class RunFrodoRun {
 
 private boolean stopped = false;
 
 public void run() {
 while (!stopped) {
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 }

Slide 47

Slide 47 text

public class RunFrodoRun {
 
 private boolean stopped = false;
 
 public void run() {
 while (!stopped) {
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 }

Slide 48

Slide 48 text

public class RunFrodoRun {
 
 private boolean stopped = false;
 
 public void run() {
 while (!stopped) {
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 }

Slide 49

Slide 49 text

public class RunFrodoRun {
 
 private boolean stopped = false;
 
 public void run() {
 while (!stopped) {
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 }

Slide 50

Slide 50 text

public class RunFrodoRun {
 
 private boolean stopped = false;
 
 public void run() {
 while (!stopped) {
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 }

Slide 51

Slide 51 text

public class RunFrodoRun {
 
 private boolean stopped = false;
 
 public void run() {
 while (!stopped) {
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 }

Slide 52

Slide 52 text

public class RunFrodoRun {
 
 private boolean stopped = false;
 
 public void run() { <- Thread 1
 while (!stopped) {
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 }

Slide 53

Slide 53 text

public class RunFrodoRun {
 
 private boolean stopped = false;
 
 public void run() {
 while (!stopped) {
 // RUN!!!
 }
 }
 
 public void stop() { <- Thread 2
 this.stopped = true;
 }
 }

Slide 54

Slide 54 text

public class RunFrodoRun {
 
 private boolean stopped = false;
 
 public void run() {
 while (!stopped) { <- Thread 1
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 }

Slide 55

Slide 55 text

public class RunFrodoRun {
 
 private boolean stopped = false;
 
 public void run() {
 while (!stopped) { <- Thread 1
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 } MAIN MEMORY stopped

Slide 56

Slide 56 text

public class RunFrodoRun {
 
 private boolean stopped = false;
 
 public void run() {
 while (!stopped) { <- Thread 1
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 } MAIN MEMORY stopped THREAD LOCAL stopped

Slide 57

Slide 57 text

public class RunFrodoRun {
 
 private boolean stopped = false;
 
 public void run() {
 while (!stopped) { <- Thread 1
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 } MAIN MEMORY stopped THREAD LOCAL stopped L1/L2 PROCESSOR CACHE stopped

Slide 58

Slide 58 text

public class RunFrodoRun {
 
 private boolean stopped = false;
 
 public void run() {
 while (!stopped) {
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 }

Slide 59

Slide 59 text

VISIBILITY PROBLEM

Slide 60

Slide 60 text

WE CAN SOOOO FIX THIS!

Slide 61

Slide 61 text

public class RunFrodoRun {
 
 private volatile boolean stopped = false;
 
 public void run() {
 while (!stopped) {
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 }

Slide 62

Slide 62 text

public class RunFrodoRun {
 
 private volatile boolean stopped = false;
 
 public void run() {
 while (!stopped) {
 // RUN!!!
 }
 }
 
 public void stop() {
 this.stopped = true;
 }
 }

Slide 63

Slide 63 text

PROGRAM 4

Slide 64

Slide 64 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1;
 orcsKilledToday = 2;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 65

Slide 65 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1;
 orcsKilledToday = 2;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 66

Slide 66 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1;
 orcsKilledToday = 2;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 67

Slide 67 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1;
 orcsKilledToday = 2;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 68

Slide 68 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1;
 orcsKilledToday = 2;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 69

Slide 69 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1;
 orcsKilledToday = 2;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 70

Slide 70 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1; <- thread 1
 orcsKilledToday = 2;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 71

Slide 71 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1;
 orcsKilledToday = 2;
 }
 
 public int getDeadOrcs() { <- Thread 2
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 72

Slide 72 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1;
 orcsKilledToday = 2;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 } Result should be 1

Slide 73

Slide 73 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1;
 orcsKilledToday = 2;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 } But the JVM can re-order incorrectly synchronized operations

Slide 74

Slide 74 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1; <- Operation 1
 orcsKilledToday = 2; <- Operation 2
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 75

Slide 75 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() { orcsKilledToday = 2; <- Operation 2
 orcsKilledYesterday = 1; <- Operation 1
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 76

Slide 76 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() { orcsKilledToday = 2; <- Thread 1
 orcsKilledYesterday = 1;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 77

Slide 77 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1;
 orcsKilledToday = 2;
 }
 
 public int getDeadOrcs() { <- Thread 2
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 78

Slide 78 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() { orcsKilledToday = 2;
 orcsKilledYesterday = 1;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 } Result in this case will be 2

Slide 79

Slide 79 text

public class OrcFunerals {
 int orcsKilledYesterday = 0;
 int orcsKilledToday = 0;
 
 public void init() { orcsKilledToday = 2;
 orcsKilledYesterday = 1;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 80

Slide 80 text

ORDERING PROBLEM

Slide 81

Slide 81 text

FIX!

Slide 82

Slide 82 text

public class OrcFunerals {
 volatile int orcsKilledYesterday = 0;
 volatile int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1;
 orcsKilledToday = 2;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 83

Slide 83 text

public class OrcFunerals {
 volatile int orcsKilledYesterday = 0;
 volatile int orcsKilledToday = 0;
 
 public void init() {
 orcsKilledYesterday = 1;
 orcsKilledToday = 2;
 }
 
 public int getDeadOrcs() {
 return orcsKilledYesterday + orcsKilledToday;
 }
 }

Slide 84

Slide 84 text

BUT WE FIXED IT, RIGHT?

Slide 85

Slide 85 text

USER

Slide 86

Slide 86 text

USER ORDERS

Slide 87

Slide 87 text

USER ORDERS PAYMENT

Slide 88

Slide 88 text

USER ORDERS PAYMENT ACTIVITY 1

Slide 89

Slide 89 text

USER ORDERS PAYMENT ACTIVITY 1 ACTIVITY 2

Slide 90

Slide 90 text

USER ORDERS PAYMENT ACTIVITY 1 ACTIVITY 2 SYNCHRONIZE

Slide 91

Slide 91 text

USER ORDERS PAYMENT ACTIVITY 1 ACTIVITY 2 ACTIVITY 3 SYNCHRONIZE

Slide 92

Slide 92 text

USER ORDERS PAYMENT ACTIVITY 1 ACTIVITY 2 ACTIVITY 3 SYNCHRONIZE CHAT

Slide 93

Slide 93 text

USER ORDERS PAYMENT ACTIVITY 1 ACTIVITY 2 ACTIVITY 3 CHAT ACTIVITY 4 SYNCHRONIZE

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

SYNCHRONIZE

Slide 96

Slide 96 text

DEADLOCK

Slide 97

Slide 97 text

SAY SYNCHRONIZE ONE MORE TIME!!!

Slide 98

Slide 98 text

CONCURRENCY IS HARD

Slide 99

Slide 99 text

OUR BRAINS ARE SMALL

Slide 100

Slide 100 text

SO WHEN YOU MIX SHARED MUTABLE STATE WITH COMPLEXITY

Slide 101

Slide 101 text

THE MUTABLE STATE MONSTER IS BORN ACTUAL PHOTO

Slide 102

Slide 102 text

CHAPTER 3: THE PROPHET

Slide 103

Slide 103 text

THERE ONCE WAS A PROPHET CALLED GORDON MOORE

Slide 104

Slide 104 text

HE CO-FOUNDED A SEMICONDUCTOR COMPANY CALLED INTEL

Slide 105

Slide 105 text

GORDON MOORE ACTUAL PHOTO

Slide 106

Slide 106 text

IN 1965, HE MADE A PROPHECY

Slide 107

Slide 107 text

THE NUMBER OF TRANSISTORS IN A DENSE INTEGRATED CIRCUIT DOUBLES APPROXIMATELY EVERY TWO YEARS GORDON MOORE MOORE’S LAW

Slide 108

Slide 108 text

AND BEHOLD, THE PROPHECY WAS TRUE

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

BUT IN THE PAST DECADE, THE PROPHECY HASN’T HELD

Slide 111

Slide 111 text

OUR COMPUTERS WON’T BECOME SIGNIFICANTLY FASTER EVERY YEAR

Slide 112

Slide 112 text

WE CANNOT STUFF MORE TRANSISTORS INTO OUR PROCESSORS ANYMORE

Slide 113

Slide 113 text

WE HAVE TO MAKE OUR PROGRAMS USE MULTIPLE CORES EFFECTIVELY

Slide 114

Slide 114 text

OUR TOOLS SUCK

Slide 115

Slide 115 text

WE NEED TO DO BETTER

Slide 116

Slide 116 text

WE’RE JUST GOING TO HAVE TO..

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

CHAPTER 4: THE FELLOWSHIP OF THE FUNCTIONAL PROGRAMMERS

Slide 119

Slide 119 text

JOHN MCCARTHY, ALONZO CHURCH, HASKELL CURRY, RICH HICKEY… ACTUAL GROUP PHOTO

Slide 120

Slide 120 text

THEY WERE THINKING OF AN ALTERNATIVE WAY OF REPRESENTING COMPUTATION

Slide 121

Slide 121 text

CAN WE REPRESENT PROGRAMS AS A SERIES OF DATA TRANSFORMATIONS INSTEAD OF A SERIES OF SEQUENTIAL DATA MUTATIONS?

Slide 122

Slide 122 text

IMPERATIVE PROGRAM

Slide 123

Slide 123 text

int[] numbers = {5,10,15};
 
 int sum() {
 int total = 0;
 
 for(int i=0; i

Slide 124

Slide 124 text

int[] numbers = {5,10,15};
 
 int sum() {
 int total = 0;
 
 for(int i=0; i

Slide 125

Slide 125 text

int[] numbers = {5,10,15};
 
 int sum() {
 int total = 0;
 
 for(int i=0; i

Slide 126

Slide 126 text

int[] numbers = {5,10,15};
 
 int sum() {
 int total = 0;
 
 for(int i=0; i

Slide 127

Slide 127 text

int[] numbers = {5,10,15};
 
 int sum() {
 int total = 0;
 
 for(int i=0; i

Slide 128

Slide 128 text

int[] numbers = {5,10,15};
 
 int sum() {
 int total = 0;
 
 for(int i=0; i

Slide 129

Slide 129 text

int[] numbers = {5,10,15};
 
 int sum() {
 int total = 0;
 
 for(int i=0; i

Slide 130

Slide 130 text

int[] numbers = {5,10,15};
 
 int sum() {
 int total = 0;
 
 for(int i=0; i

Slide 131

Slide 131 text

FUNCTIONAL PROGRAM

Slide 132

Slide 132 text

int compute() {
 return sum(sum(sum(0, 5), 10), 15);
 }
 
 int sum(int x, int y) {
 return x + y;
 }

Slide 133

Slide 133 text

int compute() {
 return sum(sum(sum(0, 5), 10), 15);
 }
 
 int sum(int x, int y) {
 return x + y;
 }

Slide 134

Slide 134 text

int compute() {
 return sum(sum(sum(0, 5), 10), 15);
 }
 
 int sum(int x, int y) {
 return x + y;
 }

Slide 135

Slide 135 text

int compute() {
 return sum(sum(sum(0, 5), 10), 15);
 }
 
 int sum(int x, int y) {
 return x + y;
 }

Slide 136

Slide 136 text

int compute() {
 return sum(sum(sum(0, 5), 10), 15);
 }
 
 int sum(int x, int y) {
 return x + y;
 }

Slide 137

Slide 137 text

int compute() {
 return sum(sum(sum(0, 5), 10), 15);
 }
 
 int sum(int x, int y) {
 return x + y;
 } The result is still 30

Slide 138

Slide 138 text

FUNCTIONAL PROGRAMMING IS ABOUT MODELLING OUR PROGRAMS IN TERMS OF EXPRESSIONS RATHER THAN STEP BY STEP INSTRUCTIONS

Slide 139

Slide 139 text

THE IDEA IS TO BE AS DECLARATIVE AS POSSIBLE AND LET THE SYSTEM WORRY ABOUT LOW LEVEL DETAILS

Slide 140

Slide 140 text

WE DON'T NEED TO KNOW EVERYTHING ABOUT FUNCTIONAL PROGRAMMING. WE JUST NEED TO STEAL SOME GOOD IDEAS

Slide 141

Slide 141 text

IDEA 1: REFERENTIAL TRANSPARENCY

Slide 142

Slide 142 text

REFERENTIAL TRANSPARENCY MEANS THERE IS NO DIFFERENCE BETWEEN THE VALUE AND THE REFERENCE TO A PIECE OF DATA

Slide 143

Slide 143 text

Book book = new Book(“Vol 2”, “Green”);

Slide 144

Slide 144 text

Book book = new Book(“Vol 2”, “Green”);

Slide 145

Slide 145 text

Book book = new Book(“Vol 2”, “Green”); book.setTitle(“Vol 3");

Slide 146

Slide 146 text

Book book = new Book(“Vol 2”, “Green”); book.setTitle(“Vol 3”); book.setColour(“Blue");

Slide 147

Slide 147 text

Book book = new Book(“Vol 2”, “Green”); book.setTitle(“Vol 3”); book.setColour(“Blue");

Slide 148

Slide 148 text

Book book = new Book(“Vol 2”, “Green”); book.setTitle(“Vol 3”); book.setColour(“Blue"); The same book object can have different values at different points in time

Slide 149

Slide 149 text

Book book = new Book(“Vol 2”, “Green”); book.setTitle(“Vol 3”); book.setColour(“Blue"); Not referentially transparent X

Slide 150

Slide 150 text

TO ACHIEVE REFERENTIAL TRANSPARENCY, WE NEED TO STEAL ANOTHER IDEA

Slide 151

Slide 151 text

IDEA 2: IMMUTABILITY

Slide 152

Slide 152 text

IMMUTABILITY MEANS DATA ONCE CREATED CANNOT BE MODIFIED

Slide 153

Slide 153 text

Book book = new Book(“Vol 2”, “Green”);

Slide 154

Slide 154 text

Book book = new Book(“Vol 2”, “Green”);

Slide 155

Slide 155 text

Book book = new Book(“Vol 2”, “Green”); book.setTitle(“Vol 3”);

Slide 156

Slide 156 text

Book book = new Book(“Vol 2”, “Green”); book.setTitle(“Vol 3”); Can’t change immutable data X

Slide 157

Slide 157 text

INSTEAD OF MODIFYING, WE CAN MAKE A WHOLE NEW OBJECT WITH OUR NEW PROPERTIES

Slide 158

Slide 158 text

MUTABLE BOOK STORE

Slide 159

Slide 159 text

public class BookStore {
 private List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public void addBooks(List additionalBooks) {
 this.books.addAll(additionalBooks);
 }
 
 public List getBooks() {
 return books;
 }
 }

Slide 160

Slide 160 text

public class BookStore {
 private List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public void addBooks(List additionalBooks) {
 this.books.addAll(additionalBooks);
 }
 
 public List getBooks() {
 return books;
 }
 }

Slide 161

Slide 161 text

public class BookStore {
 private List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public void addBooks(List additionalBooks) {
 this.books.addAll(additionalBooks);
 }
 
 public List getBooks() {
 return books;
 }
 }

Slide 162

Slide 162 text

public class BookStore {
 private List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public void addBooks(List additionalBooks) {
 this.books.addAll(additionalBooks);
 }
 
 public List getBooks() {
 return books;
 }
 }

Slide 163

Slide 163 text

public class BookStore {
 private List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public void addBooks(List additionalBooks) {
 this.books.addAll(additionalBooks);
 }
 
 public List getBooks() {
 return books;
 }
 }

Slide 164

Slide 164 text

public class BookStore {
 private List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public void addBooks(List additionalBooks) {
 this.books.addAll(additionalBooks); <- Mutation
 }
 
 public List getBooks() {
 return books;
 }
 }

Slide 165

Slide 165 text

public class BookStore {
 private List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public void addBooks(List additionalBooks) {
 this.books.addAll(additionalBooks);
 }
 
 public List getBooks() {
 return books;
 }
 } How do we make this immutable?

Slide 166

Slide 166 text

IMMUTABLE BOOK STORE

Slide 167

Slide 167 text

public class BookStore {
 private final List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public BookStore addBooks(List additionalBooks) {
 List newBooks = getBooks();
 
 newBooks.addAll(additionalBooks);
 
 return new BookStore(newBooks);
 }
 
 public List getBooks() {
 List copy = new ArrayList<>();
 for (String book : books) {
 copy.add(book);
 }
 return copy;
 }
 }

Slide 168

Slide 168 text

public class BookStore {
 private final List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public BookStore addBooks(List additionalBooks) {
 List newBooks = getBooks();
 
 newBooks.addAll(additionalBooks);
 
 return new BookStore(newBooks);
 }
 
 public List getBooks() {
 List copy = new ArrayList<>();
 for (String book : books) {
 copy.add(book);
 }
 return copy;
 }
 }

Slide 169

Slide 169 text

public class BookStore {
 private final List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public BookStore addBooks(List additionalBooks) {
 List newBooks = getBooks();
 
 newBooks.addAll(additionalBooks);
 
 return new BookStore(newBooks);
 }
 
 public List getBooks() {
 List copy = new ArrayList<>();
 for (String book : books) {
 copy.add(book);
 }
 return copy;
 }
 }

Slide 170

Slide 170 text

public class BookStore {
 private final List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public BookStore addBooks(List additionalBooks) {
 List newBooks = getBooks();
 
 newBooks.addAll(additionalBooks);
 
 return new BookStore(newBooks);
 }
 
 public List getBooks() {
 List copy = new ArrayList<>();
 for (String book : books) {
 copy.add(book);
 }
 return copy;
 }
 }

Slide 171

Slide 171 text

public class BookStore {
 private final List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public BookStore addBooks(List additionalBooks) {
 List newBooks = getBooks();
 
 newBooks.addAll(additionalBooks);
 
 return new BookStore(newBooks);
 }
 
 public List getBooks() {
 List copy = new ArrayList<>();
 for (String book : books) { <- Deep copy
 copy.add(book);
 }
 return copy;
 }
 }

Slide 172

Slide 172 text

public class BookStore {
 private final List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public BookStore addBooks(List additionalBooks) {
 List newBooks = getBooks();
 
 newBooks.addAll(additionalBooks);
 
 return new BookStore(newBooks);
 }
 
 public List getBooks() {
 List copy = new ArrayList<>();
 for (String book : books) {
 copy.add(book);
 }
 return copy; <- Return deep copy, not the original
 }
 }

Slide 173

Slide 173 text

public class BookStore {
 private final List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public BookStore addBooks(List additionalBooks) {
 List newBooks = getBooks();
 
 newBooks.addAll(additionalBooks);
 
 return new BookStore(newBooks);
 }
 
 public List getBooks() {
 List copy = new ArrayList<>();
 for (String book : books) {
 copy.add(book);
 }
 return copy;
 }
 }

Slide 174

Slide 174 text

public class BookStore {
 private final List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public BookStore addBooks(List additionalBooks) {
 List newBooks = getBooks(); <- Get deep copy
 
 newBooks.addAll(additionalBooks);
 
 return new BookStore(newBooks);
 }
 
 public List getBooks() {
 List copy = new ArrayList<>();
 for (String book : books) {
 copy.add(book);
 }
 return copy;
 }
 }

Slide 175

Slide 175 text

public class BookStore {
 private final List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public BookStore addBooks(List additionalBooks) {
 List newBooks = getBooks();
 
 newBooks.addAll(additionalBooks); <- Add to deep copy 
 
 return new BookStore(newBooks);
 }
 
 public List getBooks() {
 List copy = new ArrayList<>();
 for (String book : books) {
 copy.add(book);
 }
 return copy;
 }
 }

Slide 176

Slide 176 text

public class BookStore {
 private final List books;
 
 public BookStore(final List books) {
 this.books = books;
 }
 
 public BookStore addBooks(List additionalBooks) {
 List newBooks = getBooks();
 
 newBooks.addAll(additionalBooks);
 
 return new BookStore(newBooks); <- Return a new BookStore
 }
 
 public List getBooks() {
 List copy = new ArrayList<>();
 for (String book : books) {
 copy.add(book);
 }
 return copy;
 }
 }

Slide 177

Slide 177 text

STORE 1

Slide 178

Slide 178 text

STORE 1 ADD SOME BOOKS

Slide 179

Slide 179 text

STORE 1 ADD SOME BOOKS STORE 2

Slide 180

Slide 180 text

The original book store is always the original book store

Slide 181

Slide 181 text

Referential Transparency!

Slide 182

Slide 182 text

Referential Transparency!

Slide 183

Slide 183 text

HOW DOES THIS STUFF MAKE CONCURRENCY SAFER IN OUR APPS?

Slide 184

Slide 184 text

BOOK STORE

Slide 185

Slide 185 text

BOOK STORE

Slide 186

Slide 186 text

BOOK STORE UI Thread

Slide 187

Slide 187 text

BOOK STORE UI Thread Observable.just(bookStore)
 .map(addSomeBooks())
 .map(sortAllBooks())
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 // Update list with new BookStore
 );

Slide 188

Slide 188 text

BOOK STORE UI Thread Observable.just(bookStore)
 .map(addSomeBooks())
 .map(sortAllBooks())
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 // Update list with new BookStore
 );

Slide 189

Slide 189 text

BOOK STORE UI Thread Observable.just(bookStore)
 .map(addSomeBooks())
 .map(sortAllBooks())
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 // Update list with new BookStore
 );

Slide 190

Slide 190 text

BOOK STORE UI Thread Observable.just(bookStore)
 .map(addSomeBooks())
 .map(sortAllBooks())
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 // Update list with new BookStore
 );

Slide 191

Slide 191 text

BOOK STORE UI Thread Observable.just(bookStore)
 .map(addSomeBooks())
 .map(sortAllBooks())
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 // Update list with new BookStore
 );

Slide 192

Slide 192 text

BOOK STORE UI Thread Observable.just(bookStore)
 .map(addSomeBooks())
 .map(sortAllBooks())
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 // Update list with new BookStore
 );

Slide 193

Slide 193 text

BOOK STORE UI Thread Observable.just(bookStore)
 .map(addSomeBooks())
 .map(sortAllBooks())
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 // Update list with new BookStore
 );

Slide 194

Slide 194 text

BOOK STORE UI Thread Observable.just(bookStore)
 .map(addSomeBooks())
 .map(sortAllBooks())
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 // Update list with new BookStore
 ); BOOK STORE 1 Background Thread

Slide 195

Slide 195 text

BOOK STORE UI Thread Observable.just(bookStore)
 .map(addSomeBooks())
 .map(sortAllBooks())
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 // Update list with new BookStore
 ); BOOK STORE 1 Background Thread No incompletely modified data

Slide 196

Slide 196 text

BOOK STORE UI Thread BOOK STORE 1 Background Thread Meanwhile, Another background thread writes to our store

Slide 197

Slide 197 text

BOOK STORE UI Thread BOOK STORE 1 Background Thread It gets it’s own copy do whatever it wants BOOK STORE FOR THREAD 2 Background Thread 2

Slide 198

Slide 198 text

BOOK STORE UI Thread BOOK STORE 1 Background Thread No lock based synchronization necessary BOOK STORE FOR THREAD 2 Background Thread 2

Slide 199

Slide 199 text

BOOK STORE UI Thread Observable.just(bookStore)
 .map(addSomeBooks())
 .map(sortAllBooks())
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 // Update list with new BookStore
 ); BOOK STORE 1 Background Thread BOOK STORE 2

Slide 200

Slide 200 text

UI Thread Observable.just(bookStore)
 .map(addSomeBooks())
 .map(sortAllBooks())
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(
 // Update list with new BookStore
 ); BOOK STORE 2

Slide 201

Slide 201 text

UI Thread BOOK STORE 2

Slide 202

Slide 202 text

BUT MY POOR GARBAGE COLLECTOR!

Slide 203

Slide 203 text

CORRECTNESS IS OFTEN MORE IMPORTANT THAT RAW PERFORMANCE

Slide 204

Slide 204 text

YOU CAN USE IMMUTABILITY ONLY WHERE YOU EXPECT CONCURRENCY

Slide 205

Slide 205 text

CHAPTER 5: A NEW AGE

Slide 206

Slide 206 text

FUNCTIONAL IS GETTING POPULAR

Slide 207

Slide 207 text

No content

Slide 208

Slide 208 text

FRONTEND

Slide 209

Slide 209 text

No content

Slide 210

Slide 210 text

BACKEND

Slide 211

Slide 211 text

No content

Slide 212

Slide 212 text

WHAT ABOUT GOOD OLD ANDROID?

Slide 213

Slide 213 text

JAVA 8 ON ANDROID INTRODUCED STREAMS, LAMBDAS AND METHOD REFERENCES

Slide 214

Slide 214 text

WE HAVE RX TO TRANSFORM DATA

Slide 215

Slide 215 text

WE HAVE KOTLIN WHICH HAS FIRST CLASS SUPPORT FOR IMMUTABLE DATA

Slide 216

Slide 216 text

YOU DON’T HAVE TO DO IT ALL AT ONCE

Slide 217

Slide 217 text

AND YOU DON’T HAVE TO DO IT ALONE

Slide 218

Slide 218 text

THE FUTURE IS BRIGHT

Slide 219

Slide 219 text

THANK YOU! @anupcowkur