Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
java-concurrent-action
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
刘凯
java-concurrent-action
Commits
fda81b5c
Commit
fda81b5c
authored
Nov 30, 2018
by
刘凯
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加CompletableFutureTest
parent
28674e67
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
13 deletions
+57
-13
CompletableFutureTest.java
...va/com/example/demo/concurrent/CompletableFutureTest.java
+57
-13
No files found.
src/test/java/com/example/demo/concurrent/CompletableFutureTest.java
View file @
fda81b5c
package
com
.
example
.
demo
.
concurrent
;
import
com.fasterxml.jackson.databind.util.JSONPObject
;
import
org.assertj.core.util.Lists
;
import
java.sql.Time
;
import
java.time.LocalDateTime
;
import
java.util.List
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.Future
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeoutException
;
import
java.util.stream.Collectors
;
import
java.util.stream.IntStream
;
/**
* @author liukai
...
...
@@ -14,30 +22,34 @@ import java.util.concurrent.TimeUnit;
public
class
CompletableFutureTest
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"starttime ---> "
+
LocalDateTime
.
now
());
Long
aa
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
"starttime ---> "
+
aa
);
try
{
// Future<Double> future = getPriceAsync("1111");
// Future<Double> future = getPriceAsync2("1111");//
//
Future
<
Double
>
future
=
getPriceAsync3
(
"1111"
);
Future
<
Double
>
futureApply
=
thenApply
(
"111"
);
// Future<Double> future = getPriceAsync3("1111");
Future
<
Double
>
futurethenCombine
=
getPriceAsync5
(
"1
111"
);
// Future<Double> futureApply = thenApply("
111");
// List<Double> futurethenCombine = listCompletable("1111");//17168(50个处理) 33254(100个处理) 67361 17158 17147
List
<
Object
>
futurethenCombine
=
listCompletable2
(
"1111"
);
//17160 33236 67362 17199 17214
// System.out.println(futurethenCombine.size());
System
.
out
.
println
(
"over ---> "
+
LocalDateTime
.
now
()
);
double
aa
=
future
.
get
(
2
,
TimeUnit
.
SECONDS
);
double
bb
=
futureApply
.
get
(
2
,
TimeUnit
.
SECONDS
);
double
cc
=
futurethenCombine
.
get
(
2
,
TimeUnit
.
SECONDS
);
System
.
out
.
println
(
"getPriceAsync3 aa= "
+
aa
);
System
.
out
.
println
(
"thenApply bb= "
+
bb
);
System
.
out
.
println
(
"futurethenCombine cc= "
+
cc
);
System
.
out
.
println
(
System
.
currentTimeMillis
()-
aa
);
//
double aa = future.get(2, TimeUnit.SECONDS);
//
double bb = futureApply.get(2,TimeUnit.SECONDS);
//
double cc = futurethenCombine.get(2,TimeUnit.SECONDS);
//
System.out.println("getPriceAsync3 aa= " + aa);
//
System.out.println("thenApply bb= " + bb);
//
System.out.println("futurethenCombine cc= " + cc);
// System.out.println(future.get(3,TimeUnit.SECONDS));
Thread
.
sleep
(
1000000
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
...
...
@@ -111,7 +123,7 @@ public class CompletableFutureTest {
private
static
double
calculatePrice
(
String
product
)
{
delay
();
return
Math
.
random
()
*
product
.
charAt
(
0
)
+
product
.
charAt
(
1
)
;
return
111
;
}
...
...
@@ -123,4 +135,36 @@ public class CompletableFutureTest {
}
}
public
static
List
<
Double
>
listCompletable
(
String
product
)
{
List
<
Double
>
o
=
Lists
.
newArrayList
();
List
<
String
>
aa
=
Lists
.
newArrayList
();
IntStream
.
range
(
1
,
50
).
forEach
(
a
->
aa
.
add
(
String
.
valueOf
(
a
)));
List
<
CompletableFuture
>
a1
=
aa
.
stream
().
map
(
a
->
CompletableFuture
.
supplyAsync
(()
->
calculatePrice
(
a
))).
collect
(
Collectors
.
toList
());
for
(
int
i
=
0
;
i
<
a1
.
size
();
i
++)
{
try
{
Double
re
=
(
Double
)
a1
.
get
(
i
).
get
(
2
,
TimeUnit
.
SECONDS
);
o
.
add
(
re
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
// a1.forEach(a->{
// try {
// Double re = (Double)a.get(2,TimeUnit.SECONDS);
// o.add(re);
// } catch (Exception e) {
// e.printStackTrace();
// }
// });
return
o
;
}
public
static
List
<
Object
>
listCompletable2
(
String
product
)
{
List
<
String
>
aa
=
Lists
.
newArrayList
();
IntStream
.
range
(
1
,
50
).
forEach
(
a
->
aa
.
add
(
String
.
valueOf
(
a
)));
List
<
CompletableFuture
>
a1
=
aa
.
stream
().
map
(
a
->
CompletableFuture
.
supplyAsync
(()
->
calculatePrice
(
a
))).
collect
(
Collectors
.
toList
());
return
a1
.
stream
().
map
(
CompletableFuture:
:
join
).
collect
(
Collectors
.
toList
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment