-
Notifications
You must be signed in to change notification settings - Fork 31
/
14s-parallel-asyc.html
342 lines (255 loc) · 7.67 KB
/
14s-parallel-asyc.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
---
layout: presentation
title: "Parallel and Asynchronous Processing"
permalink: /14s-parallel-async/
---
layout: true
<footer>
<span class="icon github">
<svg version="1.1" class="github-icon-svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 16 16" enable-background="new 0 0 16 16" xml:space="preserve">
<path fill-rule="evenodd" clip-rule="evenodd" fill="#C2C2C2" d="M7.999,0.431c-4.285,0-7.76,3.474-7.76,7.761c0,3.428,2.223,6.337,5.307,7.363c0.388,0.071,0.53-0.168,0.53-0.374c0-0.184-0.007-0.672-0.01-1.32c-2.159,0.469-2.614-1.04-2.614-1.04c-0.353-0.896-0.862-1.135-0.862-1.135c-0.705-0.481,0.053-0.472,0.053-0.472c0.779,0.055,1.189,0.8,1.189,0.8c0.692,1.186,1.816,0.843,2.258,0.645c0.071-0.502,0.271-0.843,0.493-1.037C4.86,11.425,3.049,10.76,3.049,7.786c0-0.847,0.302-1.54,0.799-2.082C3.768,5.507,3.501,4.718,3.924,3.65c0,0,0.652-0.209,2.134,0.796C6.677,4.273,7.34,4.187,8,4.184c0.659,0.003,1.323,0.089,1.943,0.261c1.482-1.004,2.132-0.796,2.132-0.796c0.423,1.068,0.157,1.857,0.077,2.054c0.497,0.542,0.798,1.235,0.798,2.082c0,2.981-1.814,3.637-3.543,3.829c0.279,0.24,0.527,0.713,0.527,1.437c0,1.037-0.01,1.874-0.01,2.129c0,0.208,0.14,0.449,0.534,0.373c3.081-1.028,5.302-3.935,5.302-7.362C15.76,3.906,12.285,0.431,7.999,0.431z"/>
</svg>
</span>
<a href="https://github.com/sikoried"><span class="username">sikoried</span></a>
</footer>
---
# Parallel Processing
## Threads
Korbinian Riedhammer
---
# Process
```java
class MyProgram {
String name;
MyProgram(String name) {
this.name = name;
System.out.println("Created MyProgram: " + name);
}
void printNum(int n) {
System.out.println(name + ": " + n);
}
public static void main(String[] args) {
MyProgram mp = new MyProgram("Test");
for (int i = 0; i < 3; i++)
mp.printNum(i);
}
}
```
---
# Process
![single-process](/assets/process.svg)
---
# Bean Counters
```java
class BeanCounter {
private final String name;
private final double[] data;
BeanCounter(String name, int n) {
this.name = name;
this.data = new double [n];
}
public void run() {
System.out.println(name + " is starting...");
Arrays.sort(data);
System.out.println(name + " is done!");
}
}
```
```java
public static void main(String... args) {
BeanCounter b1 = new BeanCounter("Bureaucrat 1", 10000);
BeanCounter b2 = new BeanCounter("Bureaucrat 2", 1000);
b1.run();
b2.run();
System.out.println("main() done!");
}
```
---
# Bean Counters
![bureaucrats-1](/assets/bureaucrats.svg)
---
# Threaded Bean Counters
```java
class BeanCounter implements Runnable {
// ...
}
```
```java
public static void main(String[] args) {
BeanCounter b1 = new BeanCounter("Bureaucrat 1", 10000);
BeanCounter b2 = new BeanCounter("Bureaucrat 2", 1000);
new Thread(b1).start();
new Thread(b2).start();
System.out.println("main() done!");
}
```
---
# Threaded Bean Counters
.w80[
![bureaucrats-2](/assets/bureaucrats_001.svg)
]
---
# Threading: Examples
Multi-threaded programming is ubiquitous in modern applications:
- browser: loading multiple resources at a time using concurrent connections
- rendering multiple animations on a page/screen
- handling user interactions such as clicks or swipes
- sorting data using divide-and-conquer
- concurrent network, database and device connections
- ability to control (pause, abort) certain long-lasting processes
---
# Shared Resources
```java
class Counter {
private int c = 0;
int getCount() {
return c;
}
void increment() {
c = c + 1;
}
}
```
```java
public class TeamBeanCounter implements Runnable {
Counter c;
TeamBeanCounter(Counter c) {
this.c = c;
}
@Override
public void run() {
for (int i = 0; i < 100000; i++) {
c.increment();
}
System.out.println("Total beans: " + c.getCount());
}
}
```
---
# Shared Resources
```java
public static void main(String[] args) {
Counter c = new Counter();
new Thread(new TeamBeanCounter(c)).start();
new Thread(new TeamBeanCounter(c)).start();
new Thread(new TeamBeanCounter(c)).start();
new Thread(new TeamBeanCounter(c)).start();
}
```
```
Total beans: 362537
```
---
# Shared Resources: Inconsistent State!
.pure-table.pure-table-bordered.pure-table-striped[
| | Thread 1 | Thread 2 | _result_ |
| - | -------- | -------- | -------- |
| 1 | tmp1 = c | | tmp1 = 0 |
| 2 | | tmp2 = c | tmp2 = 0 |
| 3 | ++tmp1 | | tmp1 = 1 |
| 4 | | ++tmp2 | tmp2 = 1 |
| 5 | c = tmp1 | | c = 1 |
| 6 | | c = tmp2 | **c = 1 !** |
]
---
# Deadlock
![Deadlock](/assets/threads-deadlock.svg)
---
# Wait - Notify
![threads-wait-notify](/assets/threads-wait-notify.svg)
---
# Consumer/Producer and Synchronized Buffer
![consumer-producer](/assets/consumer-producer.png)
---
# Thread Lifecycle
![thread-lifecycle](/assets/thread-lifecycle.svg)
---
# Futures, Callables, Executor
```java
interface Callable<V> {
V call();
}
```
```java
interface Future<T> {
T get();
// ...
}
```
```java
interface ExecutorService {
void execute(Runnable command);
<T> Future<T> submit(Callable<T> task);
// ...
}
```
`ExecutorService`s provided by Java:
```java
Executors.newSingleThreadExecutor();
Executors.newCachedThreadPool(); // reuses threads
Executors.newFixedThreadPool(5); // use 5 threads
```
---
# Chaining with CompletableFuture
```java
class CompletableFuture<T> implements CompletionStage<T>, Future<T> {
static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) {
// ...
}
<U> CompletionStage<U> thenApplyAsync(Function<? super T, ? extends U> fn) {
// ...
}
<U> CompletionStage<U> thenAcceptAsync(Consumer<? super T> action) {
// ...
}
CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn) {
// ...
}
// and much more...
}
```
---
# Chaining with CompletableFuture
```java
CompletableFuture<?> cf = CompletableFuture.supplyAsync(() -> "riko493:12345")
.thenApplyAsync(creds -> {
System.out.println("Authenticating with " + creds);
return "secrettoken";
})
.thenApplyAsync(token -> {
System.out.println("Retrieving status with token=" + token);
return "in the mood for holidays";
})
.thenAccept(status -> System.out.println(status))
.exceptionally(ex -> { System.out.println("Oops, something went wrong: " + ex); return null; });
System.out.println("All done!");
```
---
# Summary (1)
Use threads to run code asynchronously and in parallel.
- _asynchronously_ means the caller/delegator immediately continues execution after queueing the task
- _parallel_ means that more than one method is executed at the same time; asynchronous methods are typcially executed in parallel.
- use `join` to wait on threads to complete
Be extra careful when threads _share resources_.
- use thread-safe containers
- protect critical sections with `synchronized`
- minimize blocking time and avoid deadlocks with inter-thread communication (`wait`, `yield`, `notify`, `notifyAll`)
---
# Summary (2)
Use `Future` to retrieve results and handle exceptions within the threads.
- `get()` will block until the thread has completed (and raise possible exception)
- `cancel()` terminates the execution of the task
- `isDone()` returns `true` if the task completed
Use `CompletableFuture` for elegant asynchronous programming.
- use `supplyAsync` to create a chainable `CompletableFuture`
- use `thenApplyAsync` to transform intermediate results
- use `thenAcceptAsyc` to consume final results (end of chain!)
- use `thenCombineAsync` to join multiple `CompletableFuture`
Use `ExecutorService`s when batch-processing large quantities of data, e.g. importing multiple files, scaling images, downloading resources, etc.
---
.center[
![berch](/assets/bergkirchweih.gif)
]
.skip[
.center[Enjoy the holiday break and Bergkirchweih!]]