-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathch18.html
478 lines (310 loc) · 30 KB
/
ch18.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Study guide for the Oracle Certified Professional, Java SE 8 Programmer Exam ">
<title>Java 8 Programmer II Study Guide: Exam 1Z0-809</title>
<link href="css/code.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="https://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="js/common-sections.js"></script>
</head>
<body>
<div class="nav"></div>
<div class="header">
<div class="title-container">
<div class="chapter-title">
<h1><i class="chapter">Chapter EIGHTEEN</i><br />
Parallel Streams</h1>
<p><br /></p>
<h3 style="text-align: center;"><i>Exam Objectives</i></h3>
<p style="text-align: center;"><i>Use parallel Streams including reduction, decomposition, merging processes, pipelines and performance.</i><br /></p>
</div>
</div>
</div>
<div class="container">
<div class="column">
<h2>What is a Parallel Stream?</h2>
<p>Until now, all the examples of this section have used sequential streams, where each element are processed one by one.</p>
<p>In contrast, parallel streams split the stream into multiple parts. Each part is processed by a different thread at the same time (in parallel).</p>
<p>Under the hood, parallel streams use the Fork/Join Framework (which we'll review in a later chapter).</p>
<p>This means that by default, the number of threads available to process parallel streams equals the number of available cores of the processor of your machine.</p>
<p>The advantage of using parallel streams over the Fork/Join Framework is that they are easier to use.</p>
<p>To create a parallel stream just use the <code>parallel()</code> method:</p>
<p><code class="java hljs">Stream<String> parallelStream =<br />
Stream.of(<span class="hljs-string">"a"</span>,<span class="hljs-string">"b"</span>,<span class="hljs-string">"c"</span>).parallel();</code></p>
<p>To create a parallel stream from a <code>Collection</code> use the <code>parallelStream()</code> method:</p>
<p><code class="java hljs">List<String> list = Arrays.asList(<span class="hljs-string">"a"</span>,<span class="hljs-string">"b"</span>,<span class="hljs-string">"c"</span>);<br />
Stream<String> parStream = list.parallelStream();</code></p>
<h2>How Parallel Streams work?</h2>
<p>Let's start with the simplest example:</p>
<p><code class="java hljs">Stream.of(<span class="hljs-string">"a"</span>,<span class="hljs-string">"b"</span>,<span class="hljs-string">"c"</span>,<span class="hljs-string">"d"</span>,<span class="hljs-string">"e"</span>)<br />
.forEach(System.out::print);</code></p>
<p>Printing a list of elements with a sequential stream will output the expected result:</p>
<p><code class="java hljs">abcde</code></p>
<p>However, when using a parallel stream:</p>
<p><code class="java hljs">Stream.of(<span class="hljs-string">"a"</span>,<span class="hljs-string">"b"</span>,<span class="hljs-string">"c"</span>,<span class="hljs-string">"d"</span>,<span class="hljs-string">"e"</span>)<br />
.parallel()<br />
.forEach(System.out::print);</code></p>
<p>The output can be different for each execution:</p>
<p><code class="java hljs">cbade <span class="hljs-comment">// One execution</span><br />
cebad <span class="hljs-comment">// Another execution</span><br />
cbdea <span class="hljs-comment">// Yet another execution</span></code></p>
<p>The reason is the decomposing of the stream on parts and their processing by different threads we talked about before.</p>
<p>So parallel streams are more appropriate for operations where the order of processing doesn't matter and that don't need to keep a state (they are stateless and independent).</p>
<p>An example to see this difference is the use of <code>findFirst()</code> vs. <code>findAny()</code>.</p>
<p>In a previous chapter, we mentioned that <code>findFirst()</code> method returns the first element of a stream. But when using parallel streams and this is decomposed in multiple parts, this method has to "know" which element is the first one:</p>
<p><code class="java hljs"><span class="hljs-keyword">long</span> start = System.nanoTime();<br />
String first = Stream.of(<span class="hljs-string">"a"</span>,<span class="hljs-string">"b"</span>,<span class="hljs-string">"c"</span>,<span class="hljs-string">"d"</span>,<span class="hljs-string">"e"</span>)<br />
.parallel().findFirst().get();<br />
<span class="hljs-keyword">double</span> duration = (System.nanoTime() - start) / <span class="hljs-number">1000000</span>;<br />
System.out.println(<br />
first + <span class="hljs-string">" found in "</span> + duration + <span class="hljs-string">" milliseconds"</span>);</code></p>
<p>The output:</p>
<p><code class="java hljs">a found in 2.436155 milliseconds</code></p>
<p>Because of that, if the order doesn't matter, it's better to use <code>findAny()</code> with parallel streams:</p>
<p><code class="java hljs"><span class="hljs-keyword">long</span> start = System.nanoTime();<br />
String any = Stream.of(<span class="hljs-string">"a"</span>,<span class="hljs-string">"b"</span>,<span class="hljs-string">"c"</span>,<span class="hljs-string">"d"</span>,<span class="hljs-string">"e"</span>)<br />
.parallel().findAny().get();<br />
<span class="hljs-keyword">double</span> duration = (System.nanoTime() - start) / <span class="hljs-number">1000000</span>;<br />
System.out.println(<br />
any + <span class="hljs-string">" found in "</span> + duration + <span class="hljs-string">" milliseconds"</span>);</code></p>
<p>The output:</p>
<p><code class="java hljs">c found in 0.063169 milliseconds</code></p>
<p>As a parallel stream is processed, well, in parallel, is reasonable to believe that it will be processed faster than a sequential stream. But as you can see with <code>findFirst()</code>, this is not always the case.</p>
<p>Stateful operations, like :</p>
<p><code class="java hljs"><span class="hljs-function">Stream<T> <span class="hljs-title">distinct</span><span class="hljs-params">()</span><br />
Stream<T> <span class="hljs-title">sorted</span><span class="hljs-params">()</span><br />
Stream<T> <span class="hljs-title">sorted</span><span class="hljs-params">(Comparator<? <span class="hljs-keyword">super</span> T> comparator)</span><br />
Stream<T> <span class="hljs-title">limit</span><span class="hljs-params">(<span class="hljs-keyword">long</span> maxSize)</span><br />
Stream<T> <span class="hljs-title">skip</span><span class="hljs-params">(<span class="hljs-keyword">long</span> n)</span></span></code></p>
<p>Incorporate state from previously processed elements and may need to go through the entire stream to produce a result, so they are not a good fit for parallel streams.</p>
<p>By the way, you can turn a parallel stream into a sequential one with the <code>sequential()</code> method:</p>
<p><code class="java hljs">stream<br />
.parallel()<br />
.filter(..)<br />
.sequential()<br />
.forEach(...);</code></p>
<p>Check if a stream is parallel with <code>isParallel()</code>:</p>
<p><code class="java hljs">stream.parallel().isParallel(); <span class="hljs-comment">// true</span></code></p>
<p>And turn an ordered stream into a unordered one (or ensure that the stream is unordered) with <code>unordered()</code>;</p>
<p><code class="java hljs">stream<br />
.parallel()<br />
.unordered()<br />
.collect(...);</code></p>
<p>But don't believe that by first executing the stateful operations and then turning the stream into a parallel one, the performance will be better in all cases, or worse, the entire operation may run in parallel, like the following example:</p>
<p><code class="java hljs"><span class="hljs-keyword">double</span> start = System.nanoTime();<br />
Stream.of(<span class="hljs-string">"b"</span>,<span class="hljs-string">"d"</span>,<span class="hljs-string">"a"</span>,<span class="hljs-string">"c"</span>,<span class="hljs-string">"e"</span>)<br />
.sorted()<br />
.filter(s -> {<br />
System.out.println(<span class="hljs-string">"Filter:"</span> + s);<br />
<span class="hljs-keyword"> return</span> !<span class="hljs-string">"d"</span>.equals(s);<br />
})<br />
.parallel()<br />
.map(s -> {<br />
System.out.println(<span class="hljs-string">"Map:"</span> + s);<br />
<span class="hljs-keyword"> return</span> s += s;<br />
})<br />
.forEach(System.out::println);<br />
<span class="hljs-keyword">double</span> duration = (System.nanoTime() - start) / 1_000_000;<br />
System.out.println(duration + <span class="hljs-string">" milliseconds"</span>);</code></p>
<p>One might think that the stream is sorted and filtered sequentially, but the output shows something else:</p>
<p><code class="java hljs">Filter:c<br />
Map:c<br />
cc<br />
Filter:a<br />
Map:a<br />
aa<br />
Filter:b<br />
Map:b<br />
bb<br />
Filter:d<br />
Filter:e<br />
Map:e<br />
ee<br />
79.470779 milliseconds</code></p>
<p>Compare this with the output of the sequential version (just comment <code>parallel()</code>):</p>
<p><code class="java hljs">Filter:a<br />
Map:a<br />
aa<br />
Filter:b<br />
Map:b<br />
bb<br />
Filter:c<br />
Map:c<br />
cc<br />
Filter:d<br />
Filter:e<br />
Map:e<br />
ee<br />
1.554562 milliseconds</code></p>
<p>In this case, the sequential version performed better.</p>
<p>But if we have an independent or stateless operation, where the order doesn't matter, let's say, counting the number of odd numbers in a large range, the parallel version will perform better:</p>
<p><code class="java hljs"><span class="hljs-keyword">double</span> start = System.nanoTime();<br />
<span class="hljs-keyword">long</span> c = IntStream.rangeClosed(<span class="hljs-number">0</span>, <span class="hljs-number">1_000_000_000</span>)<br />
.parallel()<br />
.filter(i -> i % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>)<br />
.count();<br />
<span class="hljs-keyword">double</span> duration = (System.nanoTime() - start) / <span class="hljs-number">1_000_000</span>;<br />
System.out.println(<span class="hljs-string">"Got "</span> + c + <span class="hljs-string">" in "</span> + duration + <span class="hljs-string">" milliseconds"</span>);</code></p>
<p>The parallel version output:</p>
<p><code class="java hljs">Got 500000001 in 738.678448 milliseconds</code></p>
<p>The sequential version output:</p>
<p><code class="java hljs">Got 500000001 in 1275.271882 milliseconds</code></p>
<p>In summary, parallel streams don't always perform better than sequential streams.</p>
<p>This, the fact that parallel streams process results independently and that the order cannot be guaranteed are the most important things you need to know.</p>
<p>But in practice, how do you know when to use sequential or parallel streams for better performance?</p>
<p>Here are some rules:</p>
<ul>
<li>For a small set of data, sequential streams are almost always the best choice due to the overhead of the parallelism.</li>
<li>When using parallel streams, avoid stateful (like <code>sorted()</code>) and order-based (like <code>findFirst()</code>) operations.</li>
<li>Operations that are computationally expensive (considering all the operation in the pipeline), generally have a better performance using a parallel stream.</li>
<li>When in doubt, check the performance with an appropriate benchmark.</li>
</ul>
<h2>Reducing Parallel Streams</h2>
<p>In concurrent environments, assignments are bad.</p>
<p>This is because if you mutate the state of variables (especially if they are shared by more than one thread), you may run into many troubles to avoid invalid states.</p>
<p>Consider this example, which implements the factorial of <code>10</code> in a very particular way:</p>
<p><code class="java hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Total</span></span> {<br />
<span class="hljs-keyword"> public</span> <span class="hljs-keyword">long</span> total = <span class="hljs-number">1</span>;<br />
<span class="hljs-function"><span class="hljs-keyword"> public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">multiply</span><span class="hljs-params">(<span class="hljs-keyword">long</span> n)</span></span> { total *= n; }<br />
}<br />
...<br />
Total t = <span class="hljs-keyword">new</span> Total();<br />
LongStream.rangeClosed(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>)<br />
.forEach(t::multiply);<br />
System.out.println(t.total);</code></p>
<p>Here, we are using a variable to gather the result of the factorial. The output of executing this snippet of code is:</p>
<p><code class="java hljs">3628800</code></p>
<p>However, when we turn the stream into a parallel one:</p>
<p><code class="java hljs">LongStream.rangeClosed(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>)<br />
.parallel()<br />
.forEach(t::multiply);</code></p>
<p>Sometimes we get the correct result and other times we don't.</p>
<p>The problem is caused by the multiple threads accessing the variable <code>total</code> concurrently. Yes, we can synchronize the access to this variable (as we'll see in a later chapter), but that kind of defeats the purpose of parallelism (I told you assignments are bad in concurrent environments).</p>
<p>Here's where <code>reduce()</code> comes in handy.</p>
<p>If you remember from the previous chapter, <code>reduce()</code> combines the elements of a stream into a single one.</p>
<p>With parallel streams, this method creates intermediate values and then combines them, avoiding the "ordering" problem while still allowing streams to be processed in parallel by eliminating the shared stated and keep it inside the reduction process.</p>
<p>The only requirement is that the applied reducing operation must be associative.</p>
<p>This means that the operation <code>op</code> must follow this equality:</p>
<p><code class="java hljs">(a <b>op</b> b) <b>op</b> c <b>==</b> a <b>op</b> (b <b>op</b> c)</code></p>
<p>Or:</p>
<p><code class="java hljs">a <b>op</b> b <b>op</b> c <b>op</b> d <b>==</b> (a <b>op</b> b) <b>op</b> (c <b>op</b> d)</code></p>
<p>So we can evaluate <code>(a <b>op</b> b)</code> and <code>(c <b>op</b> d)</code> in parallel.</p>
<p>Returning to our example, we can implementing it using <code>parallel()</code> and <code>reduce()</code> in this way:</p>
<p><code class="java hljs"><span class="hljs-keyword">long</span> tot = LongStream.rangeClosed(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>)<br />
.parallel()<br />
.reduce(<span class="hljs-number">1</span>, (a,b) -> a*b);<br />
System.out.println(tot);</code></p>
<p>When we execute this snippet of code, it produces the correct result every time (<code>3628800</code>).</p>
<p>And if we time the execution of the first snippet (47.216181 milliseconds) and this last one (3.094717 milliseconds), we can see a great improvement in performance. Of course, these values (and the other ones presented in this chapter) depend on the power of the machine, but you should get similar results.</p>
<p>We can also apply reduction to the example presented at the beginning of this chapter to process the string in parallel while maintaining the order:</p>
<p><code class="java hljs">String s = Stream.of(<span class="hljs-string">"a"</span>,<span class="hljs-string">"b"</span>,<span class="hljs-string">"c"</span>,<span class="hljs-string">"d"</span>,<span class="hljs-string">"e"</span>)<br />
.parallel()<br />
.reduce(<span class="hljs-string">""</span>, (s1, s2) -> s1 + s2);<br />
System.out.println(s);</code></p>
<p>The output:</p>
<p><code class="java hljs">abcde</code></p>
<p>These are simple examples, but reduction is hard to get it right sometimes, so it's best to avoid shared mutable state and use stateless and independent operations to ensure parallel streams produce the best results.</p>
<p>For instance, remember this example from the last chapter?</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span> total = IntStream.of(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>)<br />
.reduce( <span class="hljs-number">4</span>, (sum, n) -> sum + n );</code></p>
<p>In this example, the first parameter is not really an identity since <code>4+n</code> is not equal to <code>n</code>.</p>
<p>When I made the stream parallel, instead of getting the expect result (<code>25</code>), I got <code>45</code>:</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span> total = IntStream.of(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>)<br />
.parallel()<br />
.reduce( <span class="hljs-number">4</span>, (sum, n) -> sum + n );</code></p>
<p>Why?</p>
<p>Because the stream is divided into parts, and the accumulator function is applied to each one independently, which means that <code>4</code> is added not only to the first element, but to the first element of each part.</p>
<p>Also, as we also saw in the last chapter, the version of <code>reduce()</code> that takes three arguments is particularly useful in parallel streams.</p>
<p>For instance, if we take the example from the last chapter that sums the length of some strings and make it parallel:</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span> length = Stream.of(<span class="hljs-string">"Parallel"</span>, <span class="hljs-string">"streams"</span>, <span class="hljs-string">"are"</span>, <span class="hljs-string">"great"</span>)<br />
.parallel()<br />
.reduce(<span class="hljs-number">0</span>,<br />
(accumInt, str) -><br />
accumInt + str.length(), <span class="hljs-comment">//accumulator</span><br />
(accumInt1, accumInt2) -><br />
accumInt1 + accumInt2); <span class="hljs-comment">//combiner</span></code></p>
<p>We can see what happens to get the result (<code>23</code>):</p>
<p>1. The accumulator function is applied to each element in no particular order. For example:</p>
<p><code class="java hljs"><span class="hljs-number">0</span> + <span class="hljs-number">5</span> <span class="hljs-comment">// great</span><br />
<span class="hljs-number">0</span> + <span class="hljs-number">3</span> <span class="hljs-comment">// are</span><br />
<span class="hljs-number">0</span> + <span class="hljs-number">8</span> <span class="hljs-comment">// Parallel</span><br />
<span class="hljs-number">0</span> + <span class="hljs-number">7</span> <span class="hljs-comment">// streams</span></code></p>
<p>2. The results of the accumulator function are combined:</p>
<p><code class="java hljs"><span class="hljs-number">5</span> + <span class="hljs-number">3</span> <span class="hljs-comment">// 8</span><br />
<span class="hljs-number">8</span> + <span class="hljs-number">7</span> <span class="hljs-comment">// 15</span><br />
<span class="hljs-number">8</span> + <span class="hljs-number">15</span> <span class="hljs-comment">// 23</span></code></p>
<p>When we are returning a reduced value of type <code>U</code> from elements of type <code>T</code>, and in a parallel stream the elements are divided into <code>N</code> intermediate results of type <code>U</code>, it makes sense to have a function that knows how to combine those values of type <code>U</code> into a single result.</p>
<p>Because of that, if we are not using different types, the accumulator function <b>IS</b> the same as the combiner function.</p>
<p>Finally, just like <code>reduce()</code>, we can safely use <code>collect()</code> with parallel streams if we follow the same requirements of associativity and identity, like for example, for any partially accumulated result, combining it with an empty result container must produce an equivalent result.</p>
<p>Or, if we are grouping with the <code>Collectors</code> class and ordering is not important, we can use the method <code>groupingByConcurrent()</code>, the concurrent version of <code>groupingBy()</code>.</p>
<h2>Key Points</h2>
<ul>
<li>Parallel streams split the stream into multiple parts. Each part is processed by a different thread at the same time (in parallel).</li>
<li>To create a parallel stream from another stream, use the <code>parallel()</code> method.</li>
<li>To create a parallel stream from a <code>Collection</code> use the <code>parallelStream()</code> method.</li>
<li>Parallel streams are more appropriate for operations where the order of processing doesn't matter and that don't need to keep a state (they are stateless and independent).</li>
<li>You can turn a parallel stream into a sequential one with the <code>sequential()</code> method.</li>
<li>You can check if a stream is parallel with <code>isParallel()</code>.</li>
<li>You can turn an ordered stream into a unordered one (or ensure that the stream is unordered) with <code>unordered()</code>.</li>
<li>Parallel streams don't always perform better than sequential streams.</li>
<li>With parallel streams, <code>reduce()</code> creates intermediate values and then combines them, avoiding the "ordering" problem while still allowing streams to be processed in parallel by eliminating shared (mutating) state and keeping it inside the reduction process.</li>
<li>The only requirement is that the applied reducing operation must be associative: <code>(a <b>op</b> b) <b>op</b> c == a <b>op</b> (b <b>op</b> c)</code>.</li>
</ul>
<h2>Self Test</h2>
<p>1. Given:</p>
<p><code class="java hljs"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Question_18_1</span></span> {<br />
<span class="hljs-function"><span class="hljs-keyword"> public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span> {<br />
OptionalInt sum = IntStream.rangeClosed(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>)<br />
.parallel()<br />
.unordered()<br />
.reduce(Integer::sum);<br />
System.out.println(sum.orElse(<span class="hljs-number">0</span>));<br />
}<br />
}</code></p>
<p>What is the result?<br /> A. <code>0</code><br /> B. <code>55</code> is printed all the time<br /> C. Sometimes <code>55</code> is printed<br /> D. An exception occurs at runtime</p>
<p>2. Which of the following statements is true?<br /> A. You can call the method <code>parallel()</code> in a <code>Collection</code> to create a parallel stream.<br /> B. Operations that are computationally expensive, generally have a better performance using a sequential stream.<br /> C. <code>filter()</code> is a stateless method.<br /> D. Parallel streams always perform better than sequential streams.</p>
<p>3. Given:</p>
<p><code class="java hljs"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Question_18_3</span></span> {<br />
<span class="hljs-function"><span class="hljs-keyword"> public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span> {<br />
OptionalDouble avg =<br />
IntStream.rangeClosed(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>)<br />
.parallel()<br />
.average();<br />
System.out.println(avg.getAsDouble());<br />
}<br />
}</code></p>
<p>What is the result?<br /> A. <code>5.5</code> is printed all the time<br /> B. Sometimes <code>5.5</code> is printed<br /> C. Compilation fails<br /> D. An exception occurs at runtime</p>
<p>4. Given:</p>
<p><code class="java hljs"><span class="hljs-keyword">public</span> <span class="hljs-class"><span
class="hljs-keyword">class</span> <span class="hljs-title">Question_18_4</span></span> {<br />
<span class="hljs-function"><span class="hljs-keyword"> public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span> {<br />
IntStream.of(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">7</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>)<br />
.distinct()<br />
.parallel()<br />
.map(i -> i*<span class="hljs-number">2</span>)<br />
.sequential()<br />
.forEach(System.out::print)<br />
}<br />
}</code></p>
<p>What is the result?<br /> A. It can print <code>142612</code> sometimes<br /> B. <code>1133767</code><br /> C. It can print <code>3131677</code> sometimes<br /> D. <code>261412</code></p>
<div class="answers">
<a href="ch18a.html" target="_blank">Open answers page</a>
</div>
<div class="book-info"></div>
<div class="linkbox">
<div class="previous">
<a href="ch17.html">17. Peeking, Mapping, Reducing and Collecting</a>
</div>
<div class="next">
<a href="ch19.html">19. Exceptions</a>
</div>
<div style="clear:both;"></div>
</div>
</div>
</div>
</body>
</html>