From Runnable and synchronized To atomically() and parallel()
-
Upload
jose-paumard -
Category
Education
-
view
2.689 -
download
0
description
Transcript of From Runnable and synchronized To atomically() and parallel()
![Page 1: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/1.jpg)
![Page 2: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/2.jpg)
![Page 3: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/3.jpg)
![Page 4: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/4.jpg)
![Page 5: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/5.jpg)
![Page 6: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/6.jpg)
![Page 7: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/7.jpg)
![Page 8: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/8.jpg)
![Page 9: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/9.jpg)
![Page 10: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/10.jpg)
![Page 11: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/11.jpg)
![Page 12: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/12.jpg)
![Page 13: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/13.jpg)
![Page 14: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/14.jpg)
![Page 15: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/15.jpg)
![Page 16: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/16.jpg)
![Page 17: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/17.jpg)
![Page 18: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/18.jpg)
![Page 19: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/19.jpg)
3 questions
![Page 20: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/20.jpg)
2 solutions
![Page 21: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/21.jpg)
1 problem
![Page 22: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/22.jpg)
![Page 23: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/23.jpg)
Multithreading
![Page 24: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/24.jpg)
Multithreading
![Page 25: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/25.jpg)
Multithreading
![Page 26: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/26.jpg)
Multithreading
![Page 27: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/27.jpg)
Parallel computing
![Page 28: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/28.jpg)
Parallel computing
![Page 29: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/29.jpg)
Parallel computing
![Page 30: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/30.jpg)
Parallel computing
![Page 31: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/31.jpg)
Parallel computing
![Page 32: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/32.jpg)
Parallel computing
![Page 33: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/33.jpg)
Java & API
![Page 34: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/34.jpg)
Java & API
![Page 35: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/35.jpg)
http://www.flickr.com/photos/jmchuff/
![Page 36: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/36.jpg)
Multicore processors
![Page 37: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/37.jpg)
Multicore processors
![Page 38: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/38.jpg)
Multicore processors
![Page 39: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/39.jpg)
On the sofware side
![Page 40: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/40.jpg)
On the sofware side
![Page 41: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/41.jpg)
![Page 42: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/42.jpg)
![Page 43: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/43.jpg)
![Page 44: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/44.jpg)
![Page 45: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/45.jpg)
A classical example
public class Singleton {
private static Singleton instance ;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton() ;
}
return instance ;
} }
![Page 46: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/46.jpg)
A classical example
![Page 47: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/47.jpg)
A classical example
![Page 48: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/48.jpg)
public class Singleton {
private static Singleton instance ;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton() ;
}
return instance ;
} }
A classical example
![Page 49: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/49.jpg)
A classical example
public class Singleton {
private static Singleton instance ;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton() ;
}
return instance ;
} }
![Page 50: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/50.jpg)
public class Singleton {
private static Singleton instance ;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton() ;
}
return instance ;
} }
A classical example
![Page 51: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/51.jpg)
public class Singleton {
private static Singleton instance ;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton() ;
}
return instance ;
} }
A classical example
![Page 52: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/52.jpg)
A classical example
![Page 53: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/53.jpg)
A classical example
![Page 54: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/54.jpg)
public class Singleton {
private static Singleton instance ;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton() ;
}
return instance ;
} }
A classical example
![Page 55: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/55.jpg)
public class Singleton {
private static Singleton instance ;
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton() ;
}
}
}
return instance ;
}
}
A classical example
![Page 56: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/56.jpg)
public class Singleton {
private static Singleton instance ;
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton() ;
}
}
}
return instance ;
}
}
A classical example
![Page 57: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/57.jpg)
public class Singleton {
private static Singleton instance ;
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton() ;
}
}
}
return instance ;
}
}
A classical example
![Page 58: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/58.jpg)
Solution to the « data race »
![Page 59: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/59.jpg)
Solution to the « data race »
![Page 60: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/60.jpg)
Solution to the « data race »
![Page 61: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/61.jpg)
Solution to the « data race »
![Page 62: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/62.jpg)
Solution to the « data race »
![Page 63: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/63.jpg)
![Page 64: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/64.jpg)
The JLS
![Page 65: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/65.jpg)
The JLS
![Page 66: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/66.jpg)
The JLS
![Page 67: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/67.jpg)
The JLS
![Page 68: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/68.jpg)
The JLS
![Page 69: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/69.jpg)
The JLS
![Page 70: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/70.jpg)
The JLS
![Page 71: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/71.jpg)
The JLS
![Page 72: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/72.jpg)
The JLS
![Page 73: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/73.jpg)
The JLS
![Page 74: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/74.jpg)
Happens before
x = 1
r1 = x
![Page 75: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/75.jpg)
Happens before
x = 1
r1 = x
![Page 76: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/76.jpg)
Happens before
x = 1
r1 = x
![Page 77: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/77.jpg)
Happens before
r1 1
x = 1
r1 = x
![Page 78: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/78.jpg)
Happens before
![Page 79: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/79.jpg)
Happens before
![Page 80: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/80.jpg)
Example : normal read / write
private int index ;
public void gloriousIncrementation() {
index++ ;
}
public void happyTesting() {
if (index > 10) {
System.out.println("Index is BIG!") ;
}
}
![Page 81: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/81.jpg)
private int index ;
public void gloriousIncrementation() {
index++ ;
}
public void happyTesting() {
if (index > 10) {
System.out.println("Index is BIG!") ;
}
}
Example : normal read / write
![Page 82: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/82.jpg)
private volatile int index ;
public void gloriousIncrementation() {
index++ ;
}
public void happyTesting() {
if (index > 10) {
System.out.println("Index is BIG!") ;
}
}
Example : volatile read / write
![Page 83: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/83.jpg)
Problematic case
public void niceMethod() {
x = 1 ;
synchronized(lock) {
y = 1 ;
}
}
public void coolMethod() {
synchronized(lock) {
r1 = y ;
}
r2 = x ;
}
private int x, y, r1, r2 ;
private Object lock = new Object() ;
![Page 84: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/84.jpg)
public void niceMethod() {
x = 1 ;
synchronized(lock) {
y = 1 ;
}
}
public void coolMethod() {
synchronized(lock) {
r1 = y ;
}
r2 = x ;
}
private int x, y, r1, r2 ;
private Object lock = new Object() ;
Problematic case
![Page 85: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/85.jpg)
public void niceMethod() {
x = 1 ;
synchronized(lock) {
y = 1 ;
}
}
public void coolMethod() {
synchronized(lock) {
r1 = y ;
}
r2 = x ;
}
private int x, y, r1, r2 ;
private Object lock = new Object() ;
Problematic case
![Page 86: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/86.jpg)
public void niceMethod() {
x = 1 ;
synchronized(lock) {
y = 1 ;
}
}
public void coolMethod() {
synchronized(lock) {
r1 = y ;
}
r2 = x ;
}
private int x, y, r1, r2 ;
private Object lock = new Object() ;
Problematic case
r1 = 1, r2 = 1
![Page 87: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/87.jpg)
public void niceMethod() {
x = 1 ;
synchronized(lock) {
y = 1 ;
}
}
public void coolMethod() {
synchronized(lock) {
r1 = y ;
}
r2 = x ;
}
private int x, y, r1, r2 ;
private Object lock = new Object() ;
Problematic case
![Page 88: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/88.jpg)
public void niceMethod() {
x = 1 ;
synchronized(lock) {
y = 1 ;
}
}
public void coolMethod() {
synchronized(lock) {
r1 = y ;
}
r2 = x ;
}
private int x, y, r1, r2 ;
private Object lock = new Object() ;
Problematic case
![Page 89: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/89.jpg)
public void niceMethod() {
x = 1 ;
synchronized(lock) {
y = 1 ;
}
}
public void coolMethod() {
synchronized(lock) {
r1 = y ;
}
r2 = x ;
}
private int x, y, r1, r2 ;
private Object lock = new Object() ;
Problematic case
r1 = 0, r2 = ?
![Page 90: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/90.jpg)
Back to the double check locking
public class Singleton {
private static Singleton instance ;
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton() ;
}
}
}
return instance ;
}
}
![Page 91: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/91.jpg)
public class Singleton {
private static Singleton instance ;
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton() ;
}
}
}
return instance ;
}
}
Back to the double check locking
![Page 92: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/92.jpg)
public class Singleton {
private static Singleton instance ;
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton() ;
}
}
}
return instance ;
}
}
Back to the double check locking
![Page 93: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/93.jpg)
public class Singleton {
private static Singleton instance ;
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton() ;
}
}
}
return instance ;
}
}
Back to the double check locking
![Page 94: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/94.jpg)
public class Singleton {
private static volatile Singleton instance ;
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton() ;
}
}
}
return instance ;
}
}
Back to the double check locking
![Page 95: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/95.jpg)
Is there an elegant solution ?
![Page 96: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/96.jpg)
Is there an elegant solution ?
public enum Singleton {
instance ;
}
![Page 97: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/97.jpg)
Is there an elegant solution ?
public enum Singleton {
instance ;
}
![Page 98: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/98.jpg)
![Page 99: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/99.jpg)
![Page 100: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/100.jpg)
![Page 101: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/101.jpg)
![Page 102: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/102.jpg)
![Page 103: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/103.jpg)
Example
private Object o = new Object() ;
private int index = 0 ;
public void slickMethod() {
synchronized (o) {
index++ ;
}
}
![Page 104: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/104.jpg)
private Object o = new Object() ;
private int index = 0 ;
public void slickMethod() {
synchronized (o) {
index++ ;
}
}
Example
![Page 105: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/105.jpg)
private Object o = new Object() ;
private int index = 0 ;
public void slickMethod() {
synchronized (o) {
index++ ;
}
}
Example
![Page 106: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/106.jpg)
![Page 107: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/107.jpg)
![Page 108: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/108.jpg)
![Page 109: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/109.jpg)
![Page 110: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/110.jpg)
![Page 111: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/111.jpg)
Memory barrier
![Page 112: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/112.jpg)
Memory barrier
![Page 113: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/113.jpg)
![Page 114: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/114.jpg)
![Page 115: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/115.jpg)
Optimization 1
![Page 116: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/116.jpg)
Optimization 1
![Page 117: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/117.jpg)
Optimization 1
![Page 118: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/118.jpg)
Optimization 1
![Page 119: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/119.jpg)
![Page 120: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/120.jpg)
![Page 121: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/121.jpg)
Optimization 2
![Page 122: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/122.jpg)
Optimization 2
![Page 123: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/123.jpg)
Cache trap !
![Page 124: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/124.jpg)
Cache trap !
![Page 125: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/125.jpg)
![Page 126: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/126.jpg)
![Page 127: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/127.jpg)
![Page 128: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/128.jpg)
![Page 129: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/129.jpg)
![Page 130: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/130.jpg)
![Page 131: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/131.jpg)
![Page 132: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/132.jpg)
![Page 133: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/133.jpg)
![Page 134: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/134.jpg)
![Page 135: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/135.jpg)
![Page 136: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/136.jpg)
![Page 137: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/137.jpg)
False sharing
![Page 138: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/138.jpg)
False sharing
![Page 139: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/139.jpg)
Variable padding
![Page 140: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/140.jpg)
public class Sequence {
private static final AtomicLongFieldUpdater<Sequence> updater =
AtomicLongFieldUpdater.newUpdater(Sequence.class, "value");
private volatile long p1 = 7L, p2 = 7L, p3 = 7L, p4 = 7L,
p5 = 7L, p6 = 7L, p7 = 7L ;
private volatile long value = Sequencer.INITIAL_CURSOR_VALUE ;
private volatile long q1 = 7L, q2 = 7L, q3 = 7L, q4 = 7L,
q5 = 7L, q6 = 7L, q7 = 7L;
// ...
public long sumPaddingToPreventOptimisation() {
return p1 + p2 + p3 + p4 + p5 + p6 + p7 + value +
q1 + q2 + q3 + q4 + q5 + q6 + q7;
}
public void setPaddingValue(final long value) {
p1 = p2 = p3 = p4 = p5 = p6 = p7 = q1 = q2 = q3 = q4 = q5 = q6 = q7 = value;
}
}
![Page 141: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/141.jpg)
package java.util.concurrent; /** * * @since 1.7 * @author Doug Lea */ public class ThreadLocalRandom extends Random { /** * The random seed. We can't use super.seed. */ private long rnd; // Padding to help avoid memory contention among seed updates in // different TLRs in the common case that they are located near // each other. private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7; }
![Page 142: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/142.jpg)
How to write efficient concurrent code ?
![Page 143: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/143.jpg)
How to write efficient concurrent code ?
![Page 144: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/144.jpg)
![Page 145: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/145.jpg)
![Page 146: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/146.jpg)
Runnable r = new Runnable() { public void run() { while (true) { System.out.println(« Let’s go sailing !") ; } } } ; Thread t = new Thread(r) ; t.start() ;
![Page 147: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/147.jpg)
ExecutorService service = new ScheduledThreadPoolExecutor(10) ;
Callable<Boolean> task = new Callable<Boolean>() {
public Boolean call() throws Exception {
int i = 0 ;
while (i++ < 1000) {
System.out.println("Let’s go swimming !") ;
}
return true ;
}
};
Future<Boolean> f = service.submit(task) ;
Boolean b = f.get(100, TimeUnit.MILLISECONDS) ;
![Page 148: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/148.jpg)
![Page 149: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/149.jpg)
private Object o = new Object() ;
private int index = 0 ;
public void saltyMethod() {
synchronized (o) {
index++ ;
}
}
private Object o = new Object() ;
private int volatile index = 0 ;
public void pepperedMethod() {
index++ ;
}
![Page 150: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/150.jpg)
Lock interface
private Lock lock = new ReentrantLock() ; public void crispyMethod() { lock.lock() ; // blocking call as synchronized // ... lock.unlock() ; } ;
![Page 151: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/151.jpg)
Lock interface
private Lock lock = new ReentrantLock() ; public void crispyMethod() { lock.tryLock(10, TimeUnit.MILLISECONDS) ; // timeout // ... lock.unlock() ; } ;
![Page 152: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/152.jpg)
Semaphore interface
private Semaphore s = new Semaphore(5) ; public void maMethode() { s.acquire() ; // blocking call as synchronized // ... s.release() ; } ;
![Page 153: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/153.jpg)
Semaphore interface
private Semaphore s = new Semaphore(5) ; public void maMethode() { s.tryAcquire(10, TimeUnit.MILLISECONDS) ; // timeout // ... s.release() ; } ;
![Page 154: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/154.jpg)
Semaphore interface
![Page 155: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/155.jpg)
CountDownLatch Class
private CountDownLatch latch = new CountDownLatch() ; public void init() { db.connect() ; // slow operation latch.countDown() ; // openning of the latch } ;
public void process() { latch.await() ; // blocks until the latch is openned } ;
![Page 156: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/156.jpg)
CyclicBarrier Class
private CyclicBarrier barrier = new CyclicBarrier(2) ;
public void processOne() { // ... barrier.await() ; // blocks until 4 calls } ;
public void processTwo() { // ... barrier.await(10, TimeUnit.MICROSECONDS) ; } ;
![Page 157: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/157.jpg)
ReadWriteLock Interface
![Page 158: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/158.jpg)
ReadWriteLock Interface
private ReadWriteLock rwLock = new ReentrantReadWriteLock() ;
public void aMethodThatReads() { Lock readLock = rwLock.readLock() ; readLock.lock() ; // reading... readLock.unlock() ; } ;
public void aMethodThatWrites() { Lock writeLock = rwLock.writeLock() ; writeLock.lock() ; // writing... writeLock.unlock() ; } ;
![Page 159: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/159.jpg)
![Page 160: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/160.jpg)
Atomic types
private AtomicInteger index = new AtomicInteger(0) ;
public void uneMethode() {
long newValue = index.incrementAndGet() ;
}
![Page 161: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/161.jpg)
Compare and swap = CAS
![Page 162: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/162.jpg)
Compare and swap = CAS
public final long incrementAndGet() {
for (;;) {
long current = get() ;
long next = current + 1 ;
if (compareAndSet(current, next))
return next ;
}
}
![Page 163: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/163.jpg)
![Page 164: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/164.jpg)
Queue & BlockingQueue interfaces
![Page 165: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/165.jpg)
Queue & BlockingQueue interfaces
![Page 166: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/166.jpg)
Queue & BlockingQueue interfaces
![Page 167: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/167.jpg)
Queue & BlockingQueue interfaces
![Page 168: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/168.jpg)
Queue & BlockingQueue interfaces
add(e) offer(e) put(e) offer(e, time, unit)
remove() poll() take() poll(e, time, unit)
element() peek() - -
![Page 169: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/169.jpg)
![Page 170: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/170.jpg)
CopyOnWriteArrayList
![Page 171: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/171.jpg)
CopyOnWriteArrayList
![Page 172: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/172.jpg)
![Page 173: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/173.jpg)
![Page 174: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/174.jpg)
![Page 175: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/175.jpg)
![Page 176: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/176.jpg)
![Page 177: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/177.jpg)
![Page 178: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/178.jpg)
![Page 179: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/179.jpg)
![Page 180: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/180.jpg)
Immutability
![Page 181: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/181.jpg)
Immutability
![Page 182: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/182.jpg)
Immutability
![Page 183: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/183.jpg)
![Page 184: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/184.jpg)
![Page 185: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/185.jpg)
![Page 186: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/186.jpg)
![Page 187: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/187.jpg)
![Page 188: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/188.jpg)
![Page 189: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/189.jpg)
![Page 190: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/190.jpg)
![Page 191: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/191.jpg)
![Page 192: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/192.jpg)
![Page 193: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/193.jpg)
![Page 194: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/194.jpg)
![Page 195: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/195.jpg)
![Page 196: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/196.jpg)
![Page 197: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/197.jpg)
![Page 198: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/198.jpg)
![Page 199: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/199.jpg)
![Page 200: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/200.jpg)
![Page 201: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/201.jpg)
![Page 202: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/202.jpg)
![Page 203: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/203.jpg)
Try trees
![Page 204: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/204.jpg)
Immutable systems
![Page 205: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/205.jpg)
![Page 206: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/206.jpg)
![Page 207: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/207.jpg)
In databases…
begin
update ... set ... where ...
commit
![Page 208: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/208.jpg)
In databases…
begin
update ... set ... where ...
commit
![Page 209: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/209.jpg)
In databases…
![Page 210: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/210.jpg)
In databases…
![Page 211: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/211.jpg)
STM Pattern
<dependency>
<groupId>se.scalablesolutions.akka</groupId>
<artifactId>akka-kernel</artifactId>
<version>1.3.1</version>
</dependency>
<repository>
<id>Akka</id>
<name>Akka Maven2 Repository</name>
<url>http://akka.io/repository/</url>
</repository>
![Page 212: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/212.jpg)
STM Pattern
final Ref<Integer> source = new Ref<Integer>(500) ;
final Atomic<Object> atom = new Atomic<Object>() {
@Override
public Object atomically() {
source.swap(source.get() + 1) ;
return null ;
}
} ;
atom.execute() ;
![Page 213: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/213.jpg)
STM Pattern
final Ref<Integer> source = new Ref<Integer>(500) ;
final Atomic<Object> atom = new Atomic<Object>() {
@Override
public Object atomically() {
source.swap(source.get() + 1) ;
return null ;
}
} ;
atom.execute() ;
![Page 214: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/214.jpg)
STM Pattern
final Ref<Integer> source = new Ref<Integer>(500) ;
final Atomic<Object> atom = new Atomic<Object>() {
@Override
public Object atomically() {
source.swap(source.get() + 1) ;
return null ;
}
} ;
atom.execute() ;
![Page 215: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/215.jpg)
STM Pattern
![Page 216: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/216.jpg)
STM Pattern
![Page 217: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/217.jpg)
STM Pattern
List<E> queue1 = new ... ;
List<E> queue2 = new ... ;
final Ref<List<E>> refQueue1 =
new Ref<List<E>>(Collections.unmodifiableList(queue1)) ;
final Ref<List<E>> refQueue2 =
new Ref<List<E>>(Collections.unmodifiableList(queue2)) ;
![Page 218: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/218.jpg)
final Atomic<Object> atom = new Atomic<Object>() {
@Override
public Object atomically() {
// duplication
List<E> dupQ1 = new ArrayList<>(refQueue1) ;
List<E> dupQ2 = new ArrayList<>(refQueue2) ;
// transfer
E element = duqQ1.remove(...) ;
duqQ2.add(element) ;
// swapping
refQueue1.swap(Collections.unmodifiableList(dupQ1) ;
refQueue2.swap(Collections.unmodifiableList(dupQ2) ;
}
} ;
![Page 219: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/219.jpg)
STM : implementation
![Page 220: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/220.jpg)
STM : implementation
![Page 221: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/221.jpg)
STM : why is it so important ?
![Page 222: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/222.jpg)
STM : why is it so important ?
![Page 223: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/223.jpg)
STM : why is it so important ?
![Page 224: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/224.jpg)
STM : why is it so important ?
![Page 225: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/225.jpg)
STM : why is it so important ?
![Page 226: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/226.jpg)
STM : why is it so important ?
![Page 227: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/227.jpg)
STM : why is it so important ?
![Page 228: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/228.jpg)
![Page 229: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/229.jpg)
Actors
![Page 230: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/230.jpg)
Actors
![Page 231: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/231.jpg)
Akka Actors
![Page 232: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/232.jpg)
Akka Actors
![Page 233: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/233.jpg)
Akka Actors
public class Vador extends UntypedActor {
public void onReceive(Object o) throws Exception {
// o is the message
// return something ;
getContext().reply(something) ;
}
}
![Page 234: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/234.jpg)
Akka Actors
public static void main(String[] args) {
Message message = new Message() ;
ActorRef jeff =
Actors.actorOf(Vader.class).start() ;
Future future =
jeff.sendRequestReplyFuture(message) ;
// a bit further away
// beware, it’s not our good ol’ concurrent Future
result = future.get() ;
}
![Page 235: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/235.jpg)
Akka Actors
![Page 236: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/236.jpg)
public class PrimeFinderActor extends UntypedActor {
public void onReceive(Object o) throws Exception {
List<Integer> bounds = (List<Integer>)o ;
int debut = bounds.get(0) ;
int fin = bounds.get(1) ;
PrimeFactors pfs = new PrimeFactors() ;
for (int i = start ; i < end ; i++) {
PrimeFactors pfi = pfs.getPrimeFactors(i) ;
pfs.add(pfi) ;
}
// return pfs ;
getContext().reply(pfs) ;
}
}
![Page 237: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/237.jpg)
public class PrimeFactorCallable implements Callable<PrimeFactors> {
private int debut, fin ;
public PrimeFactorCallable(int debut, int fin) {
this.debut = debut ; this.fin = fin ;
}
public PrimeFactors call() throws Exception {
PrimeFactors pfs = new PrimeFactors() ;
for (int i = debut ; i < fin ; i++) {
PrimeFactors pfi = pfs.getPrimeFactors(i) ;
pfs.add(pfi) ;
}
return pfs ;
}
}
![Page 238: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/238.jpg)
public static void main(String[] args) {
Future [] futures = new Future [400] ; // it’s the Akka Future
for (int i = 0 ; i < futures.length ; i++) {
List<Integer> bound = Collections.unmodifiableList(
Arrays.asList(10*i, 10*(i + 1))) ;
ActorRef primeFactorFinder =
Actors.actorOf(PrimeFinderActor.class).start() ;
futures[i] = primeFactorFinder.sendRequestReplyFuture(bound) ;
}
PrimeFactors pfs = new PrimeFactors() ;
for (int i = 0 ; i < futures.length ; i++) {
pfs.add((PrimeFactors)futures[i].get()) ;
}
Actors.registry().shutdownAll() ;
}
![Page 239: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/239.jpg)
public static void main(String... args)
throws ExecutionException, InterruptedException {
ExecutorService es = new ScheduledThreadPoolExecutor(10) ;
Future [] futures = new Future [400] ;
for (int i = 0 ; i < futures.length ; i++) {
PrimeFactorCallable callable =
new PrimeFactorCallable(10*i, 10*(i + 1)) ;
futures[i] = es.submit(callable) ;
}
PrimeFactors pfs = new PrimeFactors() ;
for (int i = 0 ; i < futures.length ; i++) {
pfs.add((PrimeFactors)futures[i].get()) ;
}
es.shutdown() ;
}
![Page 240: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/240.jpg)
BTW, the result is…
[2^3989][3^1996][5^996][7^664][11^399][13^331][17^248][19^221][23^180][29^141][31^133][37^110][41^99][43^95][47^86][53^76][59^68][61^66][67^59]
[71^56][73^54][79^50][83^48][89^44][97^41][101^39][103^38][107^37][109^36][113^35][127^31][131^30][137^29][139^28][149^26][151^26][157^25][163^
24][167^23][173^23][179^22][181^22][191^20][193^20][197^20][199^20][211^18][223^17][227^17][229^17][233^17][239^16][241^16][251^15][257^15][263
^15][269^14][271^14][277^14][281^14][283^14][293^13][307^13][311^12][313^12][317^12][331^12][337^11][347^11][349^11][353^11][359^11][367^10][37
3^10][379^10][383^10][389^10][397^10][401^9][409^9][419^9][421^9][431^9][433^9][439^9][443^9][449^8][457^8][461^8][463^8][467^8][479^8][487^8][
491^8][499^8][503^7][509^7][521^7][523^7][541^7][547^7][557^7][563^7][569^7][571^7][577^6][587^6][593^6][599^6][601^6][607^6][613^6][617^6][619
^6][631^6][641^6][643^6][647^6][653^6][659^6][661^6][673^5][677^5][683^5][691^5][701^5][709^5][719^5][727^5][733^5][739^5][743^5][751^5][757^5]
[761^5][769^5][773^5][787^5][797^5][809^4][811^4][821^4][823^4][827^4][829^4][839^4][853^4][857^4][859^4][863^4][877^4][881^4][883^4][887^4][90
7^4][911^4][919^4][929^4][937^4][941^4][947^4][953^4][967^4][971^4][977^4][983^4][991^4][997^4][1009^3][1013^3][1019^3][1021^3][1031^3][1033^3]
[1039^3][1049^3][1051^3][1061^3][1063^3][1069^3][1087^3][1091^3][1093^3][1097^3][1103^3][1109^3][1117^3][1123^3][1129^3][1151^3][1153^3][1163^3
][1171^3][1181^3][1187^3][1193^3][1201^3][1213^3][1217^3][1223^3][1229^3][1231^3][1237^3][1249^3][1259^3][1277^3][1279^3][1283^3][1289^3][1291^
3][1297^3][1301^3][1303^3][1307^3][1319^3][1321^3][1327^3][1361^2][1367^2][1373^2][1381^2][1399^2][1409^2][1423^2][1427^2][1429^2][1433^2][1439
^2][1447^2][1451^2][1453^2][1459^2][1471^2][1481^2][1483^2][1487^2][1489^2][1493^2][1499^2][1511^2][1523^2][1531^2][1543^2][1549^2][1553^2][155
9^2][1567^2][1571^2][1579^2][1583^2][1597^2][1601^2][1607^2][1609^2][1613^2][1619^2][1621^2][1627^2][1637^2][1657^2][1663^2][1667^2][1669^2][16
93^2][1697^2][1699^2][1709^2][1721^2][1723^2][1733^2][1741^2][1747^2][1753^2][1759^2][1777^2][1783^2][1787^2][1789^2][1801^2][1811^2][1823^2][1
831^2][1847^2][1861^2][1867^2][1871^2][1873^2][1877^2][1879^2][1889^2][1901^2][1907^2][1913^2][1931^2][1933^2][1949^2][1951^2][1973^2][1979^2][
1987^2][1993^2][1997^2][1999^2][2003^1][2011^1][2017^1][2027^1][2029^1][2039^1][2053^1][2063^1][2069^1][2081^1][2083^1][2087^1][2089^1][2099^1]
[2111^1][2113^1][2129^1][2131^1][2137^1][2141^1][2143^1][2153^1][2161^1][2179^1][2203^1][2207^1][2213^1][2221^1][2237^1][2239^1][2243^1][2251^1
][2267^1][2269^1][2273^1][2281^1][2287^1][2293^1][2297^1][2309^1][2311^1][2333^1][2339^1][2341^1][2347^1][2351^1][2357^1][2371^1][2377^1][2381^
1][2383^1][2389^1][2393^1][2399^1][2411^1][2417^1][2423^1][2437^1][2441^1][2447^1][2459^1][2467^1][2473^1][2477^1][2503^1][2521^1][2531^1][2539
^1][2543^1][2549^1][2551^1][2557^1][2579^1][2591^1][2593^1][2609^1][2617^1][2621^1][2633^1][2647^1][2657^1][2659^1][2663^1][2671^1][2677^1][268
3^1][2687^1][2689^1][2693^1][2699^1][2707^1][2711^1][2713^1][2719^1][2729^1][2731^1][2741^1][2749^1][2753^1][2767^1][2777^1][2789^1][2791^1][27
97^1][2801^1][2803^1][2819^1][2833^1][2837^1][2843^1][2851^1][2857^1][2861^1][2879^1][2887^1][2897^1][2903^1][2909^1][2917^1][2927^1][2939^1][2
953^1][2957^1][2963^1][2969^1][2971^1][2999^1][3001^1][3011^1][3019^1][3023^1][3037^1][3041^1][3049^1][3061^1][3067^1][3079^1][3083^1][3089^1][
3109^1][3119^1][3121^1][3137^1][3163^1][3167^1][3169^1][3181^1][3187^1][3191^1][3203^1][3209^1][3217^1][3221^1][3229^1][3251^1][3253^1][3257^1]
[3259^1][3271^1][3299^1][3301^1][3307^1][3313^1][3319^1][3323^1][3329^1][3331^1][3343^1][3347^1][3359^1][3361^1][3371^1][3373^1][3389^1][3391^1
][3407^1][3413^1][3433^1][3449^1][3457^1][3461^1][3463^1][3467^1][3469^1][3491^1][3499^1][3511^1][3517^1][3527^1][3529^1][3533^1][3539^1][3541^
1][3547^1][3557^1][3559^1][3571^1][3581^1][3583^1][3593^1][3607^1][3613^1][3617^1][3623^1][3631^1][3637^1][3643^1][3659^1][3671^1][3673^1][3677
^1][3691^1][3697^1][3701^1][3709^1][3719^1][3727^1][3733^1][3739^1][3761^1][3767^1][3769^1][3779^1][3793^1][3797^1][3803^1][3821^1][3823^1][383
3^1][3847^1][3851^1][3853^1][3863^1][3877^1][3881^1][3889^1][3907^1][3911^1][3917^1][3919^1][3923^1][3929^1][3931^1][3943^1][3947^1][3967^1][39
89^1]
![Page 241: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/241.jpg)
Akka Actors
![Page 242: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/242.jpg)
Akka Actors + STM
![Page 243: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/243.jpg)
Akka Actors + STM
![Page 244: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/244.jpg)
public class Balance {
public final int amount ;
public Balance(int amount) {
this.amount = amount ;
}
}
public class Deposit {
public final int amount ;
public Deposit(int amount) {
this.amount = amount ;
}
}
public class Withdrawal {
public final int amount ;
public Balance(int amount) {
this.amount = amount ;
}
}
public class GetBalance {
}
![Page 245: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/245.jpg)
public class BankAccount extends UntypedTransactor {
private final Ref<Integer> balance = new Ref<Integer>(0) ;
public void atomically(final Object message) {
if (message instanceof Deposit) {
int amount = ((Deposit)message).amount ;
if (amount > 0)
balance.swap(balance.get() + amount) ;
}
if (message instanceof Withdrawal) {
int amount = ((Retrait)message).amount ;
if (amount > 0) {
if (balance.get() < amount) throw new IllegalStateException("...") ;
balance.swap(balance.get() - amount) ;
}
}
if (message instanceof GetBalance)
getContext().replySafe((new Balance(balance.get()))) ;
}
}
![Page 246: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/246.jpg)
public class BankAccount extends UntypedTransactor {
private final Ref<Integer> balance = new Ref<Integer>(0) ;
public void atomically(final Object message) {
if (message instanceof Deposit) {
int amount = ((Deposit)message).amount ;
if (amount > 0)
balance.swap(balance.get() + amount) ;
}
if (message instanceof Withdrawal) {
int amount = ((Retrait)message).amount ;
if (amount > 0) {
if (balance.get() < amount) throw new IllegalStateException("...") ;
balance.swap(balance.get() - amount) ;
}
}
if (message instanceof GetBalance)
getContext().replySafe((new Balance(balance.get()))) ;
}
}
![Page 247: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/247.jpg)
public class BankAccount extends UntypedTransactor {
private final Ref<Integer> balance = new Ref<Integer>(0) ;
public void atomically(final Object message) {
if (message instanceof Deposit) {
int amount = ((Deposit)message).amount ;
if (amount > 0)
balance.swap(balance.get() + amount) ;
}
if (message instanceof Withdrawal) {
int amount = ((Retrait)message).amount ;
if (amount > 0) {
if (balance.get() < amount) throw new IllegalStateException("...") ;
balance.swap(balance.get() - amount) ;
}
}
if (message instanceof GetBalance)
getContext().replySafe((new Balance(balance.get()))) ;
}
}
![Page 248: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/248.jpg)
public class BankAccount extends UntypedTransactor {
private final Ref<Integer> balance = new Ref<Integer>(0) ;
public void atomically(final Object message) {
if (message instanceof Deposit) {
int amount = ((Deposit)message).amount ;
if (amount > 0)
balance.swap(balance.get() + amount) ;
}
if (message instanceof Withdrawal) {
int amount = ((Retrait)message).amount ;
if (amount > 0) {
if (balance.get() < amount) throw new IllegalStateException("...") ;
balance.swap(balance.get() - amount) ;
}
}
if (message instanceof GetBalance)
getContext().replySafe((new Balance(balance.get()))) ;
}
}
![Page 249: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/249.jpg)
public class Transfer {
public final ActorRef source ;
public final ActorRef destination ;
public final int amount ;
public Transfert(
final ActorRef source,
final ActorRef destination,
final int amount) {
this.source = source ;
this.destination = destination ;
this.amount = amount ;
}
}
![Page 250: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/250.jpg)
public class BankService extends UntypedTransactor {
public Set<SendTo> coordinate(final Object message) {
if (message instanceof Transfer) {
Set<SendTo> s = new HashSet<SendTo>() ;
Transfer t = (Transfer)message ;
if (t.amount > 0) { // validation
s.add(sendTo(t.destination, new Deposit(t.amount))) ;
s.add(sendTo(t.source, new Withdrawal(t.amount))) ;
return Collections.unmodifiableSet(s) ;
}
}
return nobody() ;
}
}
![Page 251: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/251.jpg)
public class BankService extends UntypedTransactor {
public Set<SendTo> coordinate(final Object message) {
if (message instanceof Transfer) {
Set<SendTo> s = new HashSet<SendTo>() ;
Transfer t = (Transfer)message ;
if (t.amount > 0) { // validation
s.add(sendTo(t.destination, new Deposit(t.amount))) ;
s.add(sendTo(t.source, new Withdrawal(t.amount))) ;
return Collections.unmodifiableSet(s) ;
}
}
return nobody() ;
}
}
![Page 252: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/252.jpg)
public class BankService extends UntypedTransactor {
public Set<SendTo> coordinate(final Object message) {
if (message instanceof Transfer) {
Set<SendTo> s = new HashSet<SendTo>() ;
Transfer t = (Transfer)message ;
if (t.amount > 0) { // validation
s.add(sendTo(t.destination, new Deposit(t.amount))) ;
s.add(sendTo(t.source, new Withdrawal(t.amount))) ;
return Collections.unmodifiableSet(s) ;
}
}
return nobody() ;
}
}
![Page 253: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/253.jpg)
public class BankService extends UntypedTransactor {
public Set<SendTo> coordinate(final Object message) {
if (message instanceof Transfer) {
Set<SendTo> s = new HashSet<SendTo>() ;
Transfer t = (Transfer)message ;
if (t.amount > 0) { // validation
s.add(sendTo(t.destination, new Deposit(t.amount))) ;
s.add(sendTo(t.source, new Withdrawal(t.amount))) ;
return Collections.unmodifiableSet(s) ;
}
}
return nobody() ;
}
}
![Page 254: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/254.jpg)
public class BankService extends UntypedTransactor {
public Set<SendTo> coordinate(final Object message) {
if (message instanceof Transfer) {
Set<SendTo> s = new HashSet<SendTo>() ;
Transfer t = (Transfer)message ;
if (t.amount > 0) { // validation
s.add(sendTo(t.destination, new Deposit(t.amount))) ;
s.add(sendTo(t.source, new Withdrawal(t.amount))) ;
return Collections.unmodifiableSet(s) ;
}
}
return nobody() ;
}
}
![Page 255: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/255.jpg)
Akka actors + STM
![Page 256: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/256.jpg)
Akka actors + STM
![Page 257: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/257.jpg)
![Page 258: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/258.jpg)
Parallel computing in Java
![Page 259: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/259.jpg)
Parallel computing in Java
![Page 260: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/260.jpg)
Parallel computing in Java
![Page 261: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/261.jpg)
![Page 262: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/262.jpg)
Fork / Join
![Page 263: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/263.jpg)
Fork / Join
![Page 264: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/264.jpg)
![Page 265: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/265.jpg)
![Page 266: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/266.jpg)
![Page 267: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/267.jpg)
![Page 268: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/268.jpg)
![Page 269: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/269.jpg)
![Page 270: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/270.jpg)
![Page 271: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/271.jpg)
![Page 272: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/272.jpg)
![Page 273: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/273.jpg)
![Page 274: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/274.jpg)
![Page 275: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/275.jpg)
![Page 276: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/276.jpg)
![Page 277: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/277.jpg)
![Page 278: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/278.jpg)
![Page 279: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/279.jpg)
Fork / Join : patterns
ForkJoinPool pool = new ForkJoinPool() ;
PrimeFactorsFinderRecursiveTask task = new PrimeFactorsFinderRecursiveTask(1, 4000) ; ForkJoinTask<PrimeFactors> pfsTask = pool.submit(task) ; PrimeFactors pfs = pfsTask.get() ;
![Page 280: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/280.jpg)
public class PrimeFactorsFinderRecursiveTask extends RecursiveTask<PrimeFactors> {
private int start, end ;
protected PrimeFactors compute() {
PrimeFactors pfs = new PrimeFactors() ;
if (end - start > ITERATIONS) { // I’m too big
// processing
ForkJoinTask<PrimeFactors> task = ... ;
task.fork() ;
PrimeFactors pfs = task.get() ;
...
} else {
for (int i = start ; i < end ; i++) {
PrimeFactors pfi = pfs.getPrimeFactors(i) ;
pfs.add(pfi) ;
}
}
return pfs ;
}
}
![Page 281: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/281.jpg)
public class PrimeFactorsFinderRecursiveTask extends RecursiveTask<PrimeFactors> {
private int start, end ;
protected PrimeFactors compute() {
PrimeFactors pfs = new PrimeFactors() ;
if (end - start > ITERATIONS) { // I’m too big
// processing
ForkJoinTask<PrimeFactors> task = ... ;
task.fork() ;
PrimeFactors pfs = task.get() ;
...
} else {
for (int i = start ; i < end ; i++) {
PrimeFactors pfi = pfs.getPrimeFactors(i) ;
pfs.add(pfi) ;
}
}
return pfs ;
}
}
![Page 282: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/282.jpg)
public class PrimeFactorsFinderRecursiveTask extends RecursiveTask<PrimeFactors> {
private int start, end ;
protected PrimeFactors compute() {
PrimeFactors pfs = new PrimeFactors() ;
if (end - start > ITERATIONS) { // I’m too big
// processing
ForkJoinTask<PrimeFactors> task = ... ;
task.fork() ;
PrimeFactors pfs = task.get() ;
...
} else {
for (int i = start ; i < end ; i++) {
PrimeFactors pfi = pfs.getPrimeFactors(i) ;
pfs.add(pfi) ;
}
}
return pfs ;
}
}
![Page 283: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/283.jpg)
Fork / Join : patterns
![Page 284: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/284.jpg)
![Page 285: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/285.jpg)
![Page 286: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/286.jpg)
![Page 287: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/287.jpg)
![Page 288: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/288.jpg)
![Page 289: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/289.jpg)
![Page 290: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/290.jpg)
Fork / Join : patterns
![Page 291: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/291.jpg)
![Page 292: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/292.jpg)
![Page 293: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/293.jpg)
![Page 294: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/294.jpg)
// 1st strategy
if (end - start > MAX_ITERATIONS) { // I’m too big
int m = (start + end) / 2 ;
PrimeFactorsFinderTask task1 = new PrimeFactorsFinderTask(start, m) ;
PrimeFactorsFinderTask task2 = new PrimeFactorsFinderTask(m, end) ;
task1.fork() ;
task2.fork() ;
PrimeFactors pfs1 = task1.join() ;
PrimeFactors pfs2 = task2.join() ;
pfs.add(pfs1) ;
pfs.add(pfs2) ;
}
![Page 295: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/295.jpg)
// 2nd strategy
if (end - start > MAX_ITERATIONS) { // I’m too big
List<ForkJoinTask<PrimeFactors>> pfsList = new ArrayList<ForkJoinTask<PrimeFactors>>() ; for (int i = start ; i < end – MAX_ITERATIONS ; i += MAX_ITERATIONS) { PrimeFactorsFinderRecursiveTask task = new PrimeFactorsFinderRecursiveTask(i, i + MAX_ITERATIONS) ; task.fork() ; pfsList.add(task) ; } for (ForkJoinTask<PrimeFactors> task : pfsList) { PrimeFactors pfsElement = task.join() ; pfs.add(pfsElement) ; } }
![Page 296: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/296.jpg)
![Page 297: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/297.jpg)
Parallel Arrays
![Page 298: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/298.jpg)
![Page 299: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/299.jpg)
![Page 300: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/300.jpg)
![Page 301: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/301.jpg)
![Page 302: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/302.jpg)
![Page 303: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/303.jpg)
![Page 304: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/304.jpg)
![Page 305: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/305.jpg)
Parallel Arrays : patterns
ForkJoinPool pool = new ForkJoinPool() ; // package !
ParralelLongArray a = ParralelLongArray.create(size, pool) ;
![Page 306: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/306.jpg)
Parallel Arrays : patterns
a.replaceWithGeneratedValue(new LongGenerator() {
@Override
public long op() {
return rand.nextInt(100) ;
}
}) ;
![Page 307: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/307.jpg)
Parallel Arrays : patterns
Ops.LongPredicate filter = new Ops.LongPredicate() {
@Override
public boolean op(long l1) {
return l1 > 50 ;
}
}
a2 = a.withFilter(filter) ;
a2.all() ;
![Page 308: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/308.jpg)
Parallel Arrays : patterns
Ops.LongReducer reducer = new Ops.LongReducer() {
@Override
public long op(long l1, long l2) {
return l1 + l2 ;
}
}
long reducedValue = a.reduce(reducer, 0L) ;
![Page 309: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/309.jpg)
Parallel Arrays : patterns
Ops.LongOp add2 = new Ops.LongOp() {
@Override
public long op(long l1) {
return l1 + 2 ;
}
} ;
a2 = a.withMapping(add2) ;
![Page 310: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/310.jpg)
Parallel Arrays : patterns
Ops.BinaryLongOp multAB = new Ops.BinaryLongOp() {
@Override
public long op(long l1, long l2) {
return l1*l2 ;
}
} ;
a2 = a0.withMapping(multAB, a1) ;
![Page 311: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/311.jpg)
Parallel Arrays : patterns
Ops.LongReducer sum = new Ops.LongReducer() {
@Override
public long op(long l1, long l2) {
return l1 + l2 ;
}
} ;
long normSquare = a0.withMapping(multAB, a0).reduce(sum, 0L) ;
double norm = Math.sqrt(normSquare) ;
![Page 312: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/312.jpg)
Parallel Arrays : patterns
![Page 313: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/313.jpg)
![Page 314: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/314.jpg)
Java 8 Sept. 2013
![Page 315: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/315.jpg)
What are the l ?
Collection<Person> persons = ... ;
int maxAge = persons.map(p -> p.getAge()).reduce(0, Integer::max) ;
![Page 316: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/316.jpg)
What are the l ?
Collection<Person> persons = ... ;
int maxAge = persons.map(p -> p.getAge()).reduce(0, Integer::max) ;
Collection<Person> oldies =
persons.filter(p -> p.age > 40).into(new ArrayList()) ;
![Page 317: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/317.jpg)
What are the l ?
Collection<Person> persons = ... ;
int maxAge = persons.map(p -> p.getAge()).reduce(0, Integer::max) ;
Collection<Person> oldies =
persons.filter(p -> p.age > 40).into(new ArrayList()) ;
![Page 318: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/318.jpg)
What are the l ?
Collection<Person> persons = ... ;
int maxAge = persons.map(p -> p.getAge()).reduce(0, Integer::max) ;
Collection<Person> oldies =
persons.filter(p -> p.age > 40).into(new ArrayList()) ;
![Page 319: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/319.jpg)
The new Collection interface
public interface Collection<E> {
public boolean add(E e) ;
public boolean addAll(Collection<? extends E> c) ;
// the usual methods of the Collection interface
// the new stuff
public void sort(Comparator<? super E> comparator)
default Collections.sort(comparator) ;
}
![Page 320: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/320.jpg)
Supports parallel() !
Collection<Person> persons = ... ;
int maxAge = persons.parallel().map(p -> p.getAge()).reduce(0, Integer::max) ;
Collection<Person> oldies =
persons.parallel().filter(p -> p.age > 40).into(new ArrayList()) ;
![Page 321: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/321.jpg)
Spliterable Interface
public interface Spliterable<E> extends Iterable<E> {
public Iterable<E>[] splits() ;
public Iterable<E>[] splits(
int maxSplits, int minPerSplits, int maxPerSplits) ;
public int naturalSplits() ;
}
![Page 322: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/322.jpg)
Going parallel with the JDK 8
![Page 323: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/323.jpg)
![Page 324: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/324.jpg)
An anecdot
![Page 325: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/325.jpg)
An anecdot
![Page 326: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/326.jpg)
An anecdot
![Page 327: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/327.jpg)
An anecdot
![Page 328: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/328.jpg)
An anecdot
![Page 329: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/329.jpg)
An anecdot
![Page 330: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/330.jpg)
An anecdot
![Page 331: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/331.jpg)
Algorithms
![Page 332: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/332.jpg)
Algorithms
![Page 333: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/333.jpg)
Algorithms
![Page 334: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/334.jpg)
![Page 335: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/335.jpg)
Conclusions
![Page 336: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/336.jpg)
Conclusions
![Page 337: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/337.jpg)
Conclusions
![Page 338: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/338.jpg)
Conclusions
![Page 339: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/339.jpg)
Conclusions
![Page 340: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/340.jpg)
Conclusions
![Page 341: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/341.jpg)
Thank you !
![Page 342: From Runnable and synchronized To atomically() and parallel()](https://reader031.fdocuments.in/reader031/viewer/2022013011/547a5a0bb4af9fc9408b489b/html5/thumbnails/342.jpg)
Q&A