Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Mutable State Monster and How to defeat it

Anup Cowkur
October 15, 2016

The Mutable State Monster and How to defeat it

Anup Cowkur

October 15, 2016
Tweet

Other Decks in Technology

Transcript

  1. THE MUTABLE STATE MONSTER
    AND HOW TO DEFEAT IT
    ANUP COWKUR

    View Slide

  2. View Slide

  3. PROLOGUE

    View Slide

  4. WHAT IS STATE?

    View Slide

  5. 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.

    View Slide

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

    View Slide

  7. CHAPTER 1:
    THE WIZARD

    View Slide

  8. A LONG TIME AGO, THERE
    WAS A GRAND WIZARD

    View Slide

  9. JOHN
    MCCARTHY
    ACTUAL PHOTO

    View Slide

  10. HE PIONEERED
    ARTIFICIAL INTELLIGENCE

    View Slide

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

    View Slide

  12. LISP
    ACTUAL SYNTAX

    View Slide

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

    View Slide

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

    View Slide

  15. BUT WHY?

    View Slide

  16. CHAPTER 2:
    THE MONSTER

    View Slide

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

    View Slide

  18. PROGRAM 1

    View Slide

  19. public void checkAndPut(final String key,

    final String value) {

    if (!map.containsKey(key)) {

    map.put(key, value);

    }

    }

    View Slide

  20. public void checkAndPut(final String key,

    final String value) {

    if (!map.containsKey(key)) {

    map.put(key, value);

    }

    }

    View Slide

  21. public void checkAndPut(final String key,

    final String value) {

    if (!map.containsKey(key)) {

    map.put(key, value);

    }

    }

    View Slide

  22. public void checkAndPut(final String key,

    final String value) {

    if (!map.containsKey(key)) { < - Thread 1

    map.put(key, value);

    }

    }

    View Slide

  23. public void checkAndPut(final String key,

    final String value) {

    if (!map.containsKey(key)) {

    map.put(key, value);

    }

    }
    Thread 2 removes key from map

    View Slide

  24. public void checkAndPut(final String key,

    final String value) {

    if (!map.containsKey(key)) {

    map.put(key, value); <- Thread 1

    }

    }

    View Slide

  25. public void checkAndPut(final String key,

    final String value) {

    if (!map.containsKey(key)) {

    map.put(key, value);

    }

    }

    View Slide

  26. CHECK-THEN-ACT
    PROBLEM

    View Slide

  27. WE CAN FIX IT!

    View Slide

  28. public void checkAndPut(final String key,

    final String value) {

    synchronized (this) {

    if (!map.containsKey(key)) {

    map.put(key, value);

    }

    }

    }

    View Slide

  29. public void checkAndPut(final String key,

    final String value) {

    synchronized (this) {

    if (!map.containsKey(key)) {

    map.put(key, value);

    }

    }

    }

    View Slide

  30. PROGRAM 2

    View Slide

  31. public class EntCounter {

    private long count = 0;


    public long increment() {

    return ++count;

    }

    }

    View Slide

  32. public class EntCounter {

    private long count = 0;


    public long increment() {

    return ++count;

    }

    }

    View Slide

  33. public class EntCounter {

    private long count = 0;


    public long increment() {

    return ++count;

    }

    }

    View Slide

  34. public class EntCounter {

    private long count = 0;


    public long increment() {

    return ++count; <- Thread 1

    }

    }

    View Slide

  35. public class EntCounter {

    private long count = 0;


    public long increment() {

    1. Read

    return ++count; 2. Modify

    3. Write 

    }

    }

    View Slide

  36. public class EntCounter {

    private long count = 0;


    public long increment() {

    1. Read <- Thread 1

    return ++count; 2. Modify

    3. Write 

    }

    }

    View Slide

  37. 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

    View Slide

  38. public class EntCounter {

    private long count = 0;


    public long increment() {

    1. Read

    return ++count; 2. Modify <- Thread 1

    3. Write 

    }

    }

    View Slide

  39. public class EntCounter {

    private long count = 0;


    public long increment() {

    1. Read

    return ++count; 2. Modify

    3. Write <- Thread 1 

    }

    }

    View Slide

  40. public class EntCounter {

    private long count = 0;


    public long increment() {

    1. Read

    return ++count; 2. Modify

    3. Write

    }

    }

    View Slide

  41. READ-MODIFY-
    WRITE PROBLEM

    View Slide

  42. BUT WE CAN FIX
    IT!

    View Slide

  43. public class EntCounter {

    private long count = 0;


    public long increment() {

    synchronized (this) {

    return ++count;

    }

    }

    }

    View Slide

  44. public class EntCounter {

    private long count = 0;


    public long increment() {

    synchronized (this) {

    return ++count;

    }

    }

    }

    View Slide

  45. PROGRAM 3

    View Slide

  46. public class RunFrodoRun {


    private boolean stopped = false;


    public void run() {

    while (!stopped) {

    // RUN!!!

    }

    }


    public void stop() {

    this.stopped = true;

    }

    }

    View Slide

  47. public class RunFrodoRun {


    private boolean stopped = false;


    public void run() {

    while (!stopped) {

    // RUN!!!

    }

    }


    public void stop() {

    this.stopped = true;

    }

    }

    View Slide

  48. public class RunFrodoRun {


    private boolean stopped = false;


    public void run() {

    while (!stopped) {

    // RUN!!!

    }

    }


    public void stop() {

    this.stopped = true;

    }

    }

    View Slide

  49. public class RunFrodoRun {


    private boolean stopped = false;


    public void run() {

    while (!stopped) {

    // RUN!!!

    }

    }


    public void stop() {

    this.stopped = true;

    }

    }

    View Slide

  50. public class RunFrodoRun {


    private boolean stopped = false;


    public void run() {

    while (!stopped) {

    // RUN!!!

    }

    }


    public void stop() {

    this.stopped = true;

    }

    }

    View Slide

  51. public class RunFrodoRun {


    private boolean stopped = false;


    public void run() {

    while (!stopped) {

    // RUN!!!

    }

    }


    public void stop() {

    this.stopped = true;

    }

    }

    View Slide

  52. public class RunFrodoRun {


    private boolean stopped = false;


    public void run() { <- Thread 1

    while (!stopped) {

    // RUN!!!

    }

    }


    public void stop() {

    this.stopped = true;

    }

    }

    View Slide

  53. public class RunFrodoRun {


    private boolean stopped = false;


    public void run() {

    while (!stopped) {

    // RUN!!!

    }

    }


    public void stop() { <- Thread 2

    this.stopped = true;

    }

    }

    View Slide

  54. public class RunFrodoRun {


    private boolean stopped = false;


    public void run() {

    while (!stopped) { <- Thread 1

    // RUN!!!

    }

    }


    public void stop() {

    this.stopped = true;

    }

    }

    View Slide

  55. public class RunFrodoRun {


    private boolean stopped = false;


    public void run() {

    while (!stopped) { <- Thread 1

    // RUN!!!

    }

    }


    public void stop() {

    this.stopped = true;

    }

    }
    MAIN MEMORY
    stopped

    View Slide

  56. 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

    View Slide

  57. 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

    View Slide

  58. public class RunFrodoRun {


    private boolean stopped = false;


    public void run() {

    while (!stopped) {

    // RUN!!!

    }

    }


    public void stop() {

    this.stopped = true;

    }

    }

    View Slide

  59. VISIBILITY
    PROBLEM

    View Slide

  60. WE CAN SOOOO
    FIX THIS!

    View Slide

  61. public class RunFrodoRun {


    private volatile boolean stopped = false;


    public void run() {

    while (!stopped) {

    // RUN!!!

    }

    }


    public void stop() {

    this.stopped = true;

    }

    }

    View Slide

  62. public class RunFrodoRun {


    private volatile boolean stopped = false;


    public void run() {

    while (!stopped) {

    // RUN!!!

    }

    }


    public void stop() {

    this.stopped = true;

    }

    }

    View Slide

  63. PROGRAM 4

    View Slide

  64. public class OrcFunerals {

    int orcsKilledYesterday = 0;

    int orcsKilledToday = 0;


    public void init() {

    orcsKilledYesterday = 1;

    orcsKilledToday = 2;

    }


    public int getDeadOrcs() {

    return orcsKilledYesterday + orcsKilledToday;

    }

    }

    View Slide

  65. public class OrcFunerals {

    int orcsKilledYesterday = 0;

    int orcsKilledToday = 0;


    public void init() {

    orcsKilledYesterday = 1;

    orcsKilledToday = 2;

    }


    public int getDeadOrcs() {

    return orcsKilledYesterday + orcsKilledToday;

    }

    }

    View Slide

  66. public class OrcFunerals {

    int orcsKilledYesterday = 0;

    int orcsKilledToday = 0;


    public void init() {

    orcsKilledYesterday = 1;

    orcsKilledToday = 2;

    }


    public int getDeadOrcs() {

    return orcsKilledYesterday + orcsKilledToday;

    }

    }

    View Slide

  67. public class OrcFunerals {

    int orcsKilledYesterday = 0;

    int orcsKilledToday = 0;


    public void init() {

    orcsKilledYesterday = 1;

    orcsKilledToday = 2;

    }


    public int getDeadOrcs() {

    return orcsKilledYesterday + orcsKilledToday;

    }

    }

    View Slide

  68. public class OrcFunerals {

    int orcsKilledYesterday = 0;

    int orcsKilledToday = 0;


    public void init() {

    orcsKilledYesterday = 1;

    orcsKilledToday = 2;

    }


    public int getDeadOrcs() {

    return orcsKilledYesterday + orcsKilledToday;

    }

    }

    View Slide

  69. public class OrcFunerals {

    int orcsKilledYesterday = 0;

    int orcsKilledToday = 0;


    public void init() {

    orcsKilledYesterday = 1;

    orcsKilledToday = 2;

    }


    public int getDeadOrcs() {

    return orcsKilledYesterday + orcsKilledToday;

    }

    }

    View Slide

  70. public class OrcFunerals {

    int orcsKilledYesterday = 0;

    int orcsKilledToday = 0;


    public void init() {

    orcsKilledYesterday = 1; <- thread 1

    orcsKilledToday = 2;

    }


    public int getDeadOrcs() {

    return orcsKilledYesterday + orcsKilledToday;

    }

    }

    View Slide

  71. public class OrcFunerals {

    int orcsKilledYesterday = 0;

    int orcsKilledToday = 0;


    public void init() {

    orcsKilledYesterday = 1;

    orcsKilledToday = 2;

    }


    public int getDeadOrcs() { <- Thread 2

    return orcsKilledYesterday + orcsKilledToday;

    }

    }

    View Slide

  72. 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

    View Slide

  73. 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

    View Slide

  74. 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;

    }

    }

    View Slide

  75. 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;

    }

    }

    View Slide

  76. public class OrcFunerals {

    int orcsKilledYesterday = 0;

    int orcsKilledToday = 0;


    public void init() {
    orcsKilledToday = 2; <- Thread 1

    orcsKilledYesterday = 1;

    }


    public int getDeadOrcs() {

    return orcsKilledYesterday + orcsKilledToday;

    }

    }

    View Slide

  77. public class OrcFunerals {

    int orcsKilledYesterday = 0;

    int orcsKilledToday = 0;


    public void init() {

    orcsKilledYesterday = 1;

    orcsKilledToday = 2;

    }


    public int getDeadOrcs() { <- Thread 2

    return orcsKilledYesterday + orcsKilledToday;

    }

    }

    View Slide

  78. 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

    View Slide

  79. public class OrcFunerals {

    int orcsKilledYesterday = 0;

    int orcsKilledToday = 0;


    public void init() {
    orcsKilledToday = 2;

    orcsKilledYesterday = 1;

    }


    public int getDeadOrcs() {

    return orcsKilledYesterday + orcsKilledToday;

    }

    }

    View Slide

  80. ORDERING
    PROBLEM

    View Slide

  81. FIX!

    View Slide

  82. public class OrcFunerals {

    volatile int orcsKilledYesterday = 0;

    volatile int orcsKilledToday = 0;


    public void init() {

    orcsKilledYesterday = 1;

    orcsKilledToday = 2;

    }


    public int getDeadOrcs() {

    return orcsKilledYesterday + orcsKilledToday;

    }

    }

    View Slide

  83. public class OrcFunerals {

    volatile int orcsKilledYesterday = 0;

    volatile int orcsKilledToday = 0;


    public void init() {

    orcsKilledYesterday = 1;

    orcsKilledToday = 2;

    }


    public int getDeadOrcs() {

    return orcsKilledYesterday + orcsKilledToday;

    }

    }

    View Slide

  84. BUT WE FIXED IT,
    RIGHT?

    View Slide

  85. USER

    View Slide

  86. USER ORDERS

    View Slide

  87. USER ORDERS PAYMENT

    View Slide

  88. USER ORDERS PAYMENT
    ACTIVITY 1

    View Slide

  89. USER ORDERS PAYMENT
    ACTIVITY 1 ACTIVITY 2

    View Slide

  90. USER ORDERS PAYMENT
    ACTIVITY 1 ACTIVITY 2
    SYNCHRONIZE

    View Slide

  91. USER ORDERS PAYMENT
    ACTIVITY 1 ACTIVITY 2 ACTIVITY 3
    SYNCHRONIZE

    View Slide

  92. USER ORDERS PAYMENT
    ACTIVITY 1 ACTIVITY 2 ACTIVITY 3
    SYNCHRONIZE
    CHAT

    View Slide

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

    View Slide

  94. View Slide

  95. SYNCHRONIZE

    View Slide

  96. DEADLOCK

    View Slide

  97. SAY SYNCHRONIZE ONE MORE TIME!!!

    View Slide

  98. CONCURRENCY IS
    HARD

    View Slide

  99. OUR BRAINS ARE
    SMALL

    View Slide

  100. SO WHEN YOU MIX
    SHARED MUTABLE STATE
    WITH COMPLEXITY

    View Slide

  101. THE MUTABLE STATE
    MONSTER IS BORN
    ACTUAL PHOTO

    View Slide

  102. CHAPTER 3:
    THE PROPHET

    View Slide

  103. THERE ONCE WAS A
    PROPHET CALLED GORDON
    MOORE

    View Slide

  104. HE CO-FOUNDED A
    SEMICONDUCTOR
    COMPANY CALLED INTEL

    View Slide

  105. GORDON MOORE
    ACTUAL PHOTO

    View Slide

  106. IN 1965, HE MADE
    A PROPHECY

    View Slide

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

    View Slide

  108. AND BEHOLD, THE
    PROPHECY WAS TRUE

    View Slide

  109. View Slide

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

    View Slide

  111. OUR COMPUTERS WON’T
    BECOME SIGNIFICANTLY
    FASTER EVERY YEAR

    View Slide

  112. WE CANNOT STUFF MORE
    TRANSISTORS INTO OUR
    PROCESSORS ANYMORE

    View Slide

  113. WE HAVE TO MAKE OUR
    PROGRAMS USE MULTIPLE
    CORES EFFECTIVELY

    View Slide

  114. OUR TOOLS SUCK

    View Slide

  115. WE NEED TO DO
    BETTER

    View Slide

  116. WE’RE JUST GOING
    TO HAVE TO..

    View Slide

  117. View Slide

  118. CHAPTER 4:
    THE FELLOWSHIP OF THE
    FUNCTIONAL PROGRAMMERS

    View Slide

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

    View Slide

  120. THEY WERE THINKING OF AN
    ALTERNATIVE WAY OF
    REPRESENTING COMPUTATION

    View Slide

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

    View Slide

  122. IMPERATIVE
    PROGRAM

    View Slide

  123. int[] numbers = {5,10,15};


    int sum() {

    int total = 0;


    for(int i=0; itotal = total + numbers[i];

    }


    return total;

    }

    View Slide

  124. int[] numbers = {5,10,15};


    int sum() {

    int total = 0;


    for(int i=0; itotal = total + numbers[i];

    }


    return total;

    }

    View Slide

  125. int[] numbers = {5,10,15};


    int sum() {

    int total = 0;


    for(int i=0; itotal = total + numbers[i];

    }


    return total;

    }

    View Slide

  126. int[] numbers = {5,10,15};


    int sum() {

    int total = 0;


    for(int i=0; itotal = total + numbers[i];

    }


    return total;

    }

    View Slide

  127. int[] numbers = {5,10,15};


    int sum() {

    int total = 0;


    for(int i=0; itotal = total + numbers[i];

    }


    return total;

    }

    View Slide

  128. int[] numbers = {5,10,15};


    int sum() {

    int total = 0;


    for(int i=0; itotal = total + numbers[i];

    }


    return total;

    }

    View Slide

  129. int[] numbers = {5,10,15};


    int sum() {

    int total = 0;


    for(int i=0; itotal = total + numbers[i];

    }


    return total;

    }

    View Slide

  130. int[] numbers = {5,10,15};


    int sum() {

    int total = 0;


    for(int i=0; itotal = total + numbers[i];

    }


    return total;

    }
    The result is 30

    View Slide

  131. FUNCTIONAL
    PROGRAM

    View Slide

  132. int compute() {

    return sum(sum(sum(0, 5), 10), 15);

    }


    int sum(int x, int y) {

    return x + y;

    }

    View Slide

  133. int compute() {

    return sum(sum(sum(0, 5), 10), 15);

    }


    int sum(int x, int y) {

    return x + y;

    }

    View Slide

  134. int compute() {

    return sum(sum(sum(0, 5), 10), 15);

    }


    int sum(int x, int y) {

    return x + y;

    }

    View Slide

  135. int compute() {

    return sum(sum(sum(0, 5), 10), 15);

    }


    int sum(int x, int y) {

    return x + y;

    }

    View Slide

  136. int compute() {

    return sum(sum(sum(0, 5), 10), 15);

    }


    int sum(int x, int y) {

    return x + y;

    }

    View Slide

  137. int compute() {

    return sum(sum(sum(0, 5), 10), 15);

    }


    int sum(int x, int y) {

    return x + y;

    }
    The result is still 30

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  141. IDEA 1:
    REFERENTIAL
    TRANSPARENCY

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    book.setTitle(“Vol 3");

    View Slide

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

    book.setTitle(“Vol 3”);
    book.setColour(“Blue");

    View Slide

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

    book.setTitle(“Vol 3”);
    book.setColour(“Blue");

    View Slide

  148. 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

    View Slide

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

    book.setTitle(“Vol 3”);
    book.setColour(“Blue");

    Not referentially transparent
    X

    View Slide

  150. TO ACHIEVE REFERENTIAL
    TRANSPARENCY, WE NEED
    TO STEAL ANOTHER IDEA

    View Slide

  151. IDEA 2:
    IMMUTABILITY

    View Slide

  152. IMMUTABILITY MEANS
    DATA ONCE CREATED
    CANNOT BE MODIFIED

    View Slide

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

    View Slide

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

    View Slide

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

    book.setTitle(“Vol 3”);

    View Slide

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

    book.setTitle(“Vol 3”);
    Can’t change immutable data
    X

    View Slide

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

    View Slide

  158. MUTABLE BOOK
    STORE

    View Slide

  159. 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;

    }

    }

    View Slide

  160. 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;

    }

    }

    View Slide

  161. 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;

    }

    }

    View Slide

  162. 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;

    }

    }

    View Slide

  163. 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;

    }

    }

    View Slide

  164. 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;

    }

    }

    View Slide

  165. 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?

    View Slide

  166. IMMUTABLE BOOK
    STORE

    View Slide

  167. 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;

    }

    }

    View Slide

  168. 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;

    }

    }

    View Slide

  169. 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;

    }

    }

    View Slide

  170. 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;

    }

    }

    View Slide

  171. 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;

    }

    }

    View Slide

  172. 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

    }

    }

    View Slide

  173. 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;

    }

    }

    View Slide

  174. 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;

    }

    }

    View Slide

  175. 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;

    }

    }

    View Slide

  176. 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;

    }

    }

    View Slide

  177. STORE 1

    View Slide

  178. STORE 1
    ADD SOME BOOKS

    View Slide

  179. STORE 1
    ADD SOME BOOKS
    STORE 2

    View Slide

  180. The original book store is
    always the original book
    store

    View Slide

  181. Referential
    Transparency!

    View Slide

  182. Referential
    Transparency!

    View Slide

  183. HOW DOES THIS STUFF
    MAKE CONCURRENCY
    SAFER IN OUR APPS?

    View Slide

  184. BOOK STORE

    View Slide

  185. BOOK STORE

    View Slide

  186. BOOK STORE
    UI Thread

    View Slide

  187. BOOK STORE
    UI Thread
    Observable.just(bookStore)

    .map(addSomeBooks())

    .map(sortAllBooks())

    .subscribeOn(Schedulers.io())

    .observeOn(AndroidSchedulers.mainThread())

    .subscribe(

    // Update list with new BookStore

    );

    View Slide

  188. BOOK STORE
    UI Thread
    Observable.just(bookStore)

    .map(addSomeBooks())

    .map(sortAllBooks())

    .subscribeOn(Schedulers.io())

    .observeOn(AndroidSchedulers.mainThread())

    .subscribe(

    // Update list with new BookStore

    );

    View Slide

  189. BOOK STORE
    UI Thread
    Observable.just(bookStore)

    .map(addSomeBooks())

    .map(sortAllBooks())

    .subscribeOn(Schedulers.io())

    .observeOn(AndroidSchedulers.mainThread())

    .subscribe(

    // Update list with new BookStore

    );

    View Slide

  190. BOOK STORE
    UI Thread
    Observable.just(bookStore)

    .map(addSomeBooks())

    .map(sortAllBooks())

    .subscribeOn(Schedulers.io())

    .observeOn(AndroidSchedulers.mainThread())

    .subscribe(

    // Update list with new BookStore

    );

    View Slide

  191. BOOK STORE
    UI Thread
    Observable.just(bookStore)

    .map(addSomeBooks())

    .map(sortAllBooks())

    .subscribeOn(Schedulers.io())

    .observeOn(AndroidSchedulers.mainThread())

    .subscribe(

    // Update list with new BookStore

    );

    View Slide

  192. BOOK STORE
    UI Thread
    Observable.just(bookStore)

    .map(addSomeBooks())

    .map(sortAllBooks())

    .subscribeOn(Schedulers.io())

    .observeOn(AndroidSchedulers.mainThread())

    .subscribe(

    // Update list with new BookStore

    );

    View Slide

  193. BOOK STORE
    UI Thread
    Observable.just(bookStore)

    .map(addSomeBooks())

    .map(sortAllBooks())

    .subscribeOn(Schedulers.io())

    .observeOn(AndroidSchedulers.mainThread())

    .subscribe(

    // Update list with new BookStore

    );

    View Slide

  194. 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

    View Slide

  195. 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

    View Slide

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

    View Slide

  197. 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

    View Slide

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

    View Slide

  199. 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

    View Slide

  200. UI Thread
    Observable.just(bookStore)

    .map(addSomeBooks())

    .map(sortAllBooks())

    .subscribeOn(Schedulers.io())

    .observeOn(AndroidSchedulers.mainThread())

    .subscribe(

    // Update list with new BookStore

    );
    BOOK STORE
    2

    View Slide

  201. UI Thread
    BOOK STORE
    2

    View Slide

  202. BUT MY POOR
    GARBAGE COLLECTOR!

    View Slide

  203. CORRECTNESS IS OFTEN
    MORE IMPORTANT THAT
    RAW PERFORMANCE

    View Slide

  204. YOU CAN USE
    IMMUTABILITY ONLY WHERE
    YOU EXPECT CONCURRENCY

    View Slide

  205. CHAPTER 5:
    A NEW AGE

    View Slide

  206. FUNCTIONAL IS
    GETTING POPULAR

    View Slide

  207. View Slide

  208. FRONTEND

    View Slide

  209. View Slide

  210. BACKEND

    View Slide

  211. View Slide

  212. WHAT ABOUT GOOD
    OLD ANDROID?

    View Slide

  213. JAVA 8 ON ANDROID INTRODUCED
    STREAMS, LAMBDAS AND
    METHOD REFERENCES

    View Slide

  214. WE HAVE RX TO TRANSFORM
    DATA

    View Slide

  215. WE HAVE KOTLIN WHICH HAS
    FIRST CLASS SUPPORT FOR
    IMMUTABLE DATA

    View Slide

  216. YOU DON’T HAVE TO
    DO IT ALL AT ONCE

    View Slide

  217. AND YOU DON’T HAVE
    TO DO IT ALONE

    View Slide

  218. THE FUTURE IS
    BRIGHT

    View Slide

  219. THANK YOU!
    @anupcowkur

    View Slide