Share
Embed
Transcript
219 slides
-
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. -
STATE IS THE CURRENT VALUE OF DATA AT ANY POINT
OF EXECUTION OF THE PROGRAM –Me TEXT -
LISP WAS THE FIRST LANGUAGE TO HAVE AUTOMATIC MEMORY MANAGEMENT
USING A GARBAGE COLLECTOR -
IT WAS ALSO ONE OF THE FIRST LANGUAGE TO HAVE
ATOMS - DATA THAT COULD NOT BE DIVIDED FURTHER -
public void checkAndPut(final String key, final String value) { if
(!map.containsKey(key)) { map.put(key, value); } } -
public void checkAndPut(final String key, final String value) { if
(!map.containsKey(key)) { map.put(key, value); } } -
public void checkAndPut(final String key, final String value) { if
(!map.containsKey(key)) { map.put(key, value); } } -
public void checkAndPut(final String key, final String value) { if
(!map.containsKey(key)) { < - Thread 1 map.put(key, value); } } -
public void checkAndPut(final String key, final String value) { if
(!map.containsKey(key)) { map.put(key, value); } } Thread 2 removes key from map -
public void checkAndPut(final String key, final String value) { if
(!map.containsKey(key)) { map.put(key, value); <- Thread 1 } } -
public void checkAndPut(final String key, final String value) { if
(!map.containsKey(key)) { map.put(key, value); } } -
public void checkAndPut(final String key, final String value) { synchronized
(this) { if (!map.containsKey(key)) { map.put(key, value); } } } -
public void checkAndPut(final String key, final String value) { synchronized
(this) { if (!map.containsKey(key)) { map.put(key, value); } } } -
public class EntCounter { private long count = 0;
public long increment() { return ++count; } } -
public class EntCounter { private long count = 0;
public long increment() { return ++count; } } -
public class EntCounter { private long count = 0;
public long increment() { return ++count; } } -
public class EntCounter { private long count = 0;
public long increment() { return ++count; <- Thread 1 } } -
public class EntCounter { private long count = 0;
public long increment() { 1. Read return ++count; 2. Modify 3. Write } } -
public class EntCounter { private long count = 0;
public long increment() { 1. Read <- Thread 1 return ++count; 2. Modify 3. Write } } -
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 -
public class EntCounter { private long count = 0;
public long increment() { 1. Read return ++count; 2. Modify <- Thread 1 3. Write } } -
public class EntCounter { private long count = 0;
public long increment() { 1. Read return ++count; 2. Modify 3. Write <- Thread 1 } } -
public class EntCounter { private long count = 0;
public long increment() { 1. Read return ++count; 2. Modify 3. Write } } -
public class EntCounter { private long count = 0;
public long increment() { synchronized (this) { return ++count; } } } -
public class EntCounter { private long count = 0;
public long increment() { synchronized (this) { return ++count; } } } -
public class RunFrodoRun { private boolean stopped = false;
public void run() { while (!stopped) { // RUN!!! } } public void stop() { this.stopped = true; } } -
public class RunFrodoRun { private boolean stopped = false;
public void run() { while (!stopped) { // RUN!!! } } public void stop() { this.stopped = true; } } -
public class RunFrodoRun { private boolean stopped = false;
public void run() { while (!stopped) { // RUN!!! } } public void stop() { this.stopped = true; } } -
public class RunFrodoRun { private boolean stopped = false;
public void run() { while (!stopped) { // RUN!!! } } public void stop() { this.stopped = true; } } -
public class RunFrodoRun { private boolean stopped = false;
public void run() { while (!stopped) { // RUN!!! } } public void stop() { this.stopped = true; } } -
public class RunFrodoRun { private boolean stopped = false;
public void run() { while (!stopped) { // RUN!!! } } public void stop() { this.stopped = true; } } -
public class RunFrodoRun { private boolean stopped = false;
public void run() { <- Thread 1 while (!stopped) { // RUN!!! } } public void stop() { this.stopped = true; } } -
public class RunFrodoRun { private boolean stopped = false;
public void run() { while (!stopped) { // RUN!!! } } public void stop() { <- Thread 2 this.stopped = true; } } -
public class RunFrodoRun { private boolean stopped = false;
public void run() { while (!stopped) { <- Thread 1 // RUN!!! } } public void stop() { this.stopped = true; } } -
public class RunFrodoRun { private boolean stopped = false;
public void run() { while (!stopped) { <- Thread 1 // RUN!!! } } public void stop() { this.stopped = true; } } MAIN MEMORY stopped -
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 -
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 -
public class RunFrodoRun { private boolean stopped = false;
public void run() { while (!stopped) { // RUN!!! } } public void stop() { this.stopped = true; } } -
public class RunFrodoRun { private volatile boolean stopped =
false; public void run() { while (!stopped) { // RUN!!! } } public void stop() { this.stopped = true; } } -
public class RunFrodoRun { private volatile boolean stopped =
false; public void run() { while (!stopped) { // RUN!!! } } public void stop() { this.stopped = true; } } -
public class OrcFunerals { int orcsKilledYesterday = 0; int orcsKilledToday
= 0; public void init() { orcsKilledYesterday = 1; orcsKilledToday = 2; } public int getDeadOrcs() { return orcsKilledYesterday + orcsKilledToday; } } -
public class OrcFunerals { int orcsKilledYesterday = 0; int orcsKilledToday
= 0; public void init() { orcsKilledYesterday = 1; orcsKilledToday = 2; } public int getDeadOrcs() { return orcsKilledYesterday + orcsKilledToday; } } -
public class OrcFunerals { int orcsKilledYesterday = 0; int orcsKilledToday
= 0; public void init() { orcsKilledYesterday = 1; orcsKilledToday = 2; } public int getDeadOrcs() { return orcsKilledYesterday + orcsKilledToday; } } -
public class OrcFunerals { int orcsKilledYesterday = 0; int orcsKilledToday
= 0; public void init() { orcsKilledYesterday = 1; orcsKilledToday = 2; } public int getDeadOrcs() { return orcsKilledYesterday + orcsKilledToday; } } -
public class OrcFunerals { int orcsKilledYesterday = 0; int orcsKilledToday
= 0; public void init() { orcsKilledYesterday = 1; orcsKilledToday = 2; } public int getDeadOrcs() { return orcsKilledYesterday + orcsKilledToday; } } -
public class OrcFunerals { int orcsKilledYesterday = 0; int orcsKilledToday
= 0; public void init() { orcsKilledYesterday = 1; orcsKilledToday = 2; } public int getDeadOrcs() { return orcsKilledYesterday + orcsKilledToday; } } -
public class OrcFunerals { int orcsKilledYesterday = 0; int orcsKilledToday
= 0; public void init() { orcsKilledYesterday = 1; <- thread 1 orcsKilledToday = 2; } public int getDeadOrcs() { return orcsKilledYesterday + orcsKilledToday; } } -
public class OrcFunerals { int orcsKilledYesterday = 0; int orcsKilledToday
= 0; public void init() { orcsKilledYesterday = 1; orcsKilledToday = 2; } public int getDeadOrcs() { <- Thread 2 return orcsKilledYesterday + orcsKilledToday; } } -
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 -
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 -
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; } } -
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; } } -
public class OrcFunerals { int orcsKilledYesterday = 0; int orcsKilledToday
= 0; public void init() { orcsKilledToday = 2; <- Thread 1 orcsKilledYesterday = 1; } public int getDeadOrcs() { return orcsKilledYesterday + orcsKilledToday; } } -
public class OrcFunerals { int orcsKilledYesterday = 0; int orcsKilledToday
= 0; public void init() { orcsKilledYesterday = 1; orcsKilledToday = 2; } public int getDeadOrcs() { <- Thread 2 return orcsKilledYesterday + orcsKilledToday; } } -
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 -
public class OrcFunerals { int orcsKilledYesterday = 0; int orcsKilledToday
= 0; public void init() { orcsKilledToday = 2; orcsKilledYesterday = 1; } public int getDeadOrcs() { return orcsKilledYesterday + orcsKilledToday; } } -
public class OrcFunerals { volatile int orcsKilledYesterday = 0; volatile
int orcsKilledToday = 0; public void init() { orcsKilledYesterday = 1; orcsKilledToday = 2; } public int getDeadOrcs() { return orcsKilledYesterday + orcsKilledToday; } } -
public class OrcFunerals { volatile int orcsKilledYesterday = 0; volatile
int orcsKilledToday = 0; public void init() { orcsKilledYesterday = 1; orcsKilledToday = 2; } public int getDeadOrcs() { return orcsKilledYesterday + orcsKilledToday; } } -
USER ORDERS PAYMENT ACTIVITY 1 ACTIVITY 2 ACTIVITY 3 CHAT
ACTIVITY 4 SYNCHRONIZE -
THE NUMBER OF TRANSISTORS IN A DENSE INTEGRATED CIRCUIT DOUBLES
APPROXIMATELY EVERY TWO YEARS GORDON MOORE MOORE’S LAW -
CAN WE REPRESENT PROGRAMS AS A SERIES OF DATA TRANSFORMATIONS
INSTEAD OF A SERIES OF SEQUENTIAL DATA MUTATIONS? -
int[] numbers = {5,10,15}; int sum() { int total
= 0; for(int i=0; i<numbers.length ; i++) { total = total + numbers[i]; } return total; } -
int[] numbers = {5,10,15}; int sum() { int total
= 0; for(int i=0; i<numbers.length ; i++) { total = total + numbers[i]; } return total; } -
int[] numbers = {5,10,15}; int sum() { int total
= 0; for(int i=0; i<numbers.length ; i++) { total = total + numbers[i]; } return total; } -
int[] numbers = {5,10,15}; int sum() { int total
= 0; for(int i=0; i<numbers.length ; i++) { total = total + numbers[i]; } return total; } -
int[] numbers = {5,10,15}; int sum() { int total
= 0; for(int i=0; i<numbers.length ; i++) { total = total + numbers[i]; } return total; } -
int[] numbers = {5,10,15}; int sum() { int total
= 0; for(int i=0; i<numbers.length ; i++) { total = total + numbers[i]; } return total; } -
int[] numbers = {5,10,15}; int sum() { int total
= 0; for(int i=0; i<numbers.length ; i++) { total = total + numbers[i]; } return total; } -
int[] numbers = {5,10,15}; int sum() { int total
= 0; for(int i=0; i<numbers.length ; i++) { total = total + numbers[i]; } return total; } The result is 30 -
int compute() { return sum(sum(sum(0, 5), 10), 15); }
int sum(int x, int y) { return x + y; } -
int compute() { return sum(sum(sum(0, 5), 10), 15); }
int sum(int x, int y) { return x + y; } -
int compute() { return sum(sum(sum(0, 5), 10), 15); }
int sum(int x, int y) { return x + y; } -
int compute() { return sum(sum(sum(0, 5), 10), 15); }
int sum(int x, int y) { return x + y; } -
int compute() { return sum(sum(sum(0, 5), 10), 15); }
int sum(int x, int y) { return x + y; } -
int compute() { return sum(sum(sum(0, 5), 10), 15); }
int sum(int x, int y) { return x + y; } The result is still 30 -
FUNCTIONAL PROGRAMMING IS ABOUT MODELLING OUR PROGRAMS IN TERMS OF
EXPRESSIONS RATHER THAN STEP BY STEP INSTRUCTIONS -
THE IDEA IS TO BE AS DECLARATIVE AS POSSIBLE AND
LET THE SYSTEM WORRY ABOUT LOW LEVEL DETAILS -
WE DON'T NEED TO KNOW EVERYTHING ABOUT FUNCTIONAL PROGRAMMING. WE
JUST NEED TO STEAL SOME GOOD IDEAS -
REFERENTIAL TRANSPARENCY MEANS THERE IS NO DIFFERENCE BETWEEN THE VALUE
AND THE REFERENCE TO A PIECE OF DATA -
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 -
Book book = new Book(“Vol 2”, “Green”); book.setTitle(“Vol 3”); book.setColour(“Blue");
Not referentially transparent X -
Book book = new Book(“Vol 2”, “Green”); book.setTitle(“Vol 3”); Can’t
change immutable data X -
INSTEAD OF MODIFYING, WE CAN MAKE A WHOLE NEW OBJECT
WITH OUR NEW PROPERTIES -
public class BookStore { private List<String> books; public BookStore(final
List<String> books) { this.books = books; } public void addBooks(List<String> additionalBooks) { this.books.addAll(additionalBooks); } public List<String> getBooks() { return books; } } -
public class BookStore { private List<String> books; public BookStore(final
List<String> books) { this.books = books; } public void addBooks(List<String> additionalBooks) { this.books.addAll(additionalBooks); } public List<String> getBooks() { return books; } } -
public class BookStore { private List<String> books; public BookStore(final
List<String> books) { this.books = books; } public void addBooks(List<String> additionalBooks) { this.books.addAll(additionalBooks); } public List<String> getBooks() { return books; } } -
public class BookStore { private List<String> books; public BookStore(final
List<String> books) { this.books = books; } public void addBooks(List<String> additionalBooks) { this.books.addAll(additionalBooks); } public List<String> getBooks() { return books; } } -
public class BookStore { private List<String> books; public BookStore(final
List<String> books) { this.books = books; } public void addBooks(List<String> additionalBooks) { this.books.addAll(additionalBooks); } public List<String> getBooks() { return books; } } -
public class BookStore { private List<String> books; public BookStore(final
List<String> books) { this.books = books; } public void addBooks(List<String> additionalBooks) { this.books.addAll(additionalBooks); <- Mutation } public List<String> getBooks() { return books; } } -
public class BookStore { private List<String> books; public BookStore(final
List<String> books) { this.books = books; } public void addBooks(List<String> additionalBooks) { this.books.addAll(additionalBooks); } public List<String> getBooks() { return books; } } How do we make this immutable? -
public class BookStore { private final List<String> books; public
BookStore(final List<String> books) { this.books = books; } public BookStore addBooks(List<String> additionalBooks) { List<String> newBooks = getBooks(); newBooks.addAll(additionalBooks); return new BookStore(newBooks); } public List<String> getBooks() { List<String> copy = new ArrayList<>(); for (String book : books) { copy.add(book); } return copy; } } -
public class BookStore { private final List<String> books; public
BookStore(final List<String> books) { this.books = books; } public BookStore addBooks(List<String> additionalBooks) { List<String> newBooks = getBooks(); newBooks.addAll(additionalBooks); return new BookStore(newBooks); } public List<String> getBooks() { List<String> copy = new ArrayList<>(); for (String book : books) { copy.add(book); } return copy; } } -
public class BookStore { private final List<String> books; public
BookStore(final List<String> books) { this.books = books; } public BookStore addBooks(List<String> additionalBooks) { List<String> newBooks = getBooks(); newBooks.addAll(additionalBooks); return new BookStore(newBooks); } public List<String> getBooks() { List<String> copy = new ArrayList<>(); for (String book : books) { copy.add(book); } return copy; } } -
public class BookStore { private final List<String> books; public
BookStore(final List<String> books) { this.books = books; } public BookStore addBooks(List<String> additionalBooks) { List<String> newBooks = getBooks(); newBooks.addAll(additionalBooks); return new BookStore(newBooks); } public List<String> getBooks() { List<String> copy = new ArrayList<>(); for (String book : books) { copy.add(book); } return copy; } } -
public class BookStore { private final List<String> books; public
BookStore(final List<String> books) { this.books = books; } public BookStore addBooks(List<String> additionalBooks) { List<String> newBooks = getBooks(); newBooks.addAll(additionalBooks); return new BookStore(newBooks); } public List<String> getBooks() { List<String> copy = new ArrayList<>(); for (String book : books) { <- Deep copy copy.add(book); } return copy; } } -
public class BookStore { private final List<String> books; public
BookStore(final List<String> books) { this.books = books; } public BookStore addBooks(List<String> additionalBooks) { List<String> newBooks = getBooks(); newBooks.addAll(additionalBooks); return new BookStore(newBooks); } public List<String> getBooks() { List<String> copy = new ArrayList<>(); for (String book : books) { copy.add(book); } return copy; <- Return deep copy, not the original } } -
public class BookStore { private final List<String> books; public
BookStore(final List<String> books) { this.books = books; } public BookStore addBooks(List<String> additionalBooks) { List<String> newBooks = getBooks(); newBooks.addAll(additionalBooks); return new BookStore(newBooks); } public List<String> getBooks() { List<String> copy = new ArrayList<>(); for (String book : books) { copy.add(book); } return copy; } } -
public class BookStore { private final List<String> books; public
BookStore(final List<String> books) { this.books = books; } public BookStore addBooks(List<String> additionalBooks) { List<String> newBooks = getBooks(); <- Get deep copy newBooks.addAll(additionalBooks); return new BookStore(newBooks); } public List<String> getBooks() { List<String> copy = new ArrayList<>(); for (String book : books) { copy.add(book); } return copy; } } -
public class BookStore { private final List<String> books; public
BookStore(final List<String> books) { this.books = books; } public BookStore addBooks(List<String> additionalBooks) { List<String> newBooks = getBooks(); newBooks.addAll(additionalBooks); <- Add to deep copy return new BookStore(newBooks); } public List<String> getBooks() { List<String> copy = new ArrayList<>(); for (String book : books) { copy.add(book); } return copy; } } -
public class BookStore { private final List<String> books; public
BookStore(final List<String> books) { this.books = books; } public BookStore addBooks(List<String> additionalBooks) { List<String> newBooks = getBooks(); newBooks.addAll(additionalBooks); return new BookStore(newBooks); <- Return a new BookStore } public List<String> getBooks() { List<String> copy = new ArrayList<>(); for (String book : books) { copy.add(book); } return copy; } } -
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 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 -
BOOK STORE UI Thread BOOK STORE 1 Background Thread Meanwhile,
Another background thread writes to our store -
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 -
BOOK STORE UI Thread BOOK STORE 1 Background Thread No
lock based synchronization necessary BOOK STORE FOR THREAD 2 Background Thread 2 -
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