FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/concurrent/CachedThreadPool3.java at master · CSapprenticeWT/OnJava8-Examples · GitHub
CSapprenticeWT
OnJava8-Examples
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OnJava8-Examples
/
concurrent
/
CachedThreadPool3.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
47 lines (46 loc) · 1.21 KB
Breadcrumbs
OnJava8-Examples
/
concurrent
/
CachedThreadPool3.java
Copy path
File metadata and controls
47 lines (46 loc) · 1.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// concurrent/CachedThreadPool3.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import
java
.
util
.*;
import
java
.
util
.
concurrent
.*;
import
java
.
util
.
stream
.*;
public
class
CachedThreadPool3
{
public
static
Integer
extractResult
(
Future
<
Integer
>
f
) {
try
{
return
f
.
get
();
}
catch
(
Exception
e
) {
throw
new
RuntimeException
(
e
);
}
}
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
ExecutorService
exec
=
Executors
.
newCachedThreadPool
();
List
<
CountingTask
>
tasks
=
IntStream
.
range
(
0
,
10
)
.
mapToObj
(
CountingTask
::
new
)
.
collect
(
Collectors
.
toList
());
List
<
Future
<
Integer
>>
futures
=
exec
.
invokeAll
(
tasks
);
Integer
sum
=
futures
.
stream
()
.
map
(
CachedThreadPool3
::
extractResult
)
.
reduce
(
0
,
Integer
::
sum
);
System
.
out
.
println
(
"sum = "
+
sum
);
exec
.
shutdown
();
}
}
/* Output:
0 pool-1-thread-1 100
9 pool-1-thread-1 100
1 pool-1-thread-2 100
2 pool-1-thread-3 100
3 pool-1-thread-4 100
6 pool-1-thread-7 100
7 pool-1-thread-8 100
4 pool-1-thread-5 100
5 pool-1-thread-6 100
8 pool-1-thread-9 100
sum = 1000
*/
Back
|
FazBrowse Home
|
New Git URL