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
28674e67
Commit
28674e67
authored
Nov 28, 2018
by
刘凯
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加CompletableFutureTest
parent
41cfce62
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
128 additions
and
1 deletion
+128
-1
CompletableFutureTest.java
...va/com/example/demo/concurrent/CompletableFutureTest.java
+126
-0
UseFuture.java
src/test/java/com/example/demo/concurrent/UseFuture.java
+2
-1
No files found.
src/test/java/com/example/demo/concurrent/CompletableFutureTest.java
0 → 100644
View file @
28674e67
package
com
.
example
.
demo
.
concurrent
;
import
java.sql.Time
;
import
java.time.LocalDateTime
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.concurrent.Future
;
import
java.util.concurrent.TimeUnit
;
/**
* @author liukai
* @Description:
* @date 2018/11/278:06 PM
*/
public
class
CompletableFutureTest
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"starttime ---> "
+
LocalDateTime
.
now
());
try
{
// Future<Double> future = getPriceAsync("1111");
// Future<Double> future = getPriceAsync2("1111");//
//
Future
<
Double
>
future
=
getPriceAsync3
(
"1111"
);
Future
<
Double
>
futureApply
=
thenApply
(
"111"
);
Future
<
Double
>
futurethenCombine
=
getPriceAsync5
(
"1111"
);
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(future.get(3,TimeUnit.SECONDS));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
public
static
CompletableFuture
<
Double
>
thenApply
(
String
product
)
throws
Exception
{
return
getPriceAsync3
(
product
).
thenApply
(
a
->
{
System
.
out
.
println
(
a
);
return
a
+
1
;
});
}
public
static
Future
<
Double
>
getPriceAsync
(
String
product
)
{
CompletableFuture
<
Double
>
futurePrice
=
new
CompletableFuture
<>();
new
Thread
(()
->
{
double
price
=
calculatePrice
(
product
);
int
a
=
1
/
0
;
futurePrice
.
complete
(
price
);
}).
start
();
return
futurePrice
;
}
public
static
Future
<
Double
>
getPriceAsync2
(
String
product
)
{
CompletableFuture
<
Double
>
futurePrice
=
new
CompletableFuture
<>();
new
Thread
(()
->
{
try
{
double
price
=
calculatePrice
(
product
);
int
a
=
1
/
0
;
futurePrice
.
complete
(
price
);
}
catch
(
Exception
e
)
{
futurePrice
.
completeExceptionally
(
e
);
}
}).
start
();
return
futurePrice
;
}
/**
* @author 工厂方法
*/
public
static
CompletableFuture
<
Double
>
getPriceAsync3
(
String
product
)
{
CompletableFuture
<
Double
>
futurePrice
=
CompletableFuture
.
supplyAsync
(()
->
calculatePrice
(
product
));
return
futurePrice
;
}
public
static
Future
<
Double
>
getPriceAsync4
(
String
product
)
{
CompletableFuture
<
Double
>
futurePrice
=
new
CompletableFuture
<>();
new
Thread
(()
->
{
try
{
double
price
=
calculatePrice
(
product
);
futurePrice
.
complete
(
price
);
}
catch
(
Exception
e
)
{
futurePrice
.
completeExceptionally
(
e
);
}
}).
start
();
return
futurePrice
;
}
public
static
Future
<
Double
>
getPriceAsync5
(
String
product
)
{
return
CompletableFuture
.
supplyAsync
(()
->
calculatePrice
(
product
)).
thenCombine
(
CompletableFuture
.
supplyAsync
(()
->
calculatePrice
(
product
)),(
a
,
b
)->
{
System
.
out
.
println
(
"getPriceAsync5 a = "
+
a
);
System
.
out
.
println
(
"getPriceAsync5 b = "
+
b
);
return
a
+
b
;
});
}
private
static
double
calculatePrice
(
String
product
)
{
delay
();
return
Math
.
random
()
*
product
.
charAt
(
0
)
+
product
.
charAt
(
1
);
}
public
static
void
delay
()
{
try
{
Thread
.
sleep
(
1000L
);
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
src/test/java/com/example/demo/concurrent/UseFuture.java
View file @
28674e67
...
...
@@ -5,6 +5,7 @@ import java.util.concurrent.ExecutorService;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
import
java.util.concurrent.FutureTask
;
import
java.util.concurrent.TimeUnit
;
public
class
UseFuture
implements
Callable
<
String
>{
private
String
para
;
...
...
@@ -48,7 +49,7 @@ public class UseFuture implements Callable<String>{
e
.
printStackTrace
();
}
//调用获取数据方法,如果call()方法没有执行完成,则依然会进行等待
System
.
out
.
println
(
"数据:"
+
future
.
get
());
System
.
out
.
println
(
"数据:"
+
future
.
get
(
1
,
TimeUnit
.
SECONDS
));
System
.
out
.
println
(
"数据:"
+
future2
.
get
());
executor
.
shutdown
();
...
...
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