File tree Expand file tree Collapse file tree 6 files changed +140
-0
lines changed
src/main/java/javaStudy/basic/callBack Expand file tree Collapse file tree 6 files changed +140
-0
lines changed Original file line number Diff line number Diff line change 6969 </exclusion >
7070 </exclusions >
7171 </dependency >
72+ <dependency >
73+ <groupId >junit</groupId >
74+ <artifactId >junit</artifactId >
75+ <version >4.13.2</version >
76+ <scope >compile</scope >
77+ </dependency >
7278 </dependencies >
7379
7480 <build >
Original file line number Diff line number Diff line change 1+ package javaStudy .basic .callBack ;
2+
3+ import org .junit .Test ;
4+
5+ import java .util .Random ;
6+ import java .util .concurrent .*;
7+
8+ public class CallBack {
9+ //这里简单地使用future和callable实现了线程执行完后
10+ public static void main (String [] args ) throws ExecutionException , InterruptedException {
11+ // 创建一个可缓存的线程池,用于执行异步任务
12+ ExecutorService executor = Executors .newCachedThreadPool ();
13+ Future <String > future = executor .submit (new Callable <String >() {
14+ @ Override
15+ public String call () throws Exception {
16+ System .out .println ("call" );
17+ TimeUnit .SECONDS .sleep (1 );
18+ return "str" ;
19+ }
20+ });
21+ //手动阻塞调用get通过call方法获得返回值。
22+ System .out .println (future .get ());
23+ //需要手动关闭,不然线程池的线程会继续执行。
24+ executor .shutdown ();
25+
26+ //使用futuretask同时作为线程执行单元和数据请求单元。
27+ FutureTask <Integer > futureTask = new FutureTask (new Callable <Integer >() {
28+ @ Override
29+ public Integer call () throws Exception {
30+ System .out .println ("dasds" );
31+ return new Random ().nextInt ();
32+ }
33+ });
34+ new Thread (futureTask ).start ();
35+ //阻塞获取返回值
36+ System .out .println (futureTask .get ());
37+ }
38+
39+ @ Test
40+ public void test () {
41+ Callable callable = new Callable () {
42+ @ Override
43+ public Object call () throws Exception {
44+ return null ;
45+ }
46+ };
47+ FutureTask futureTask = new FutureTask (callable );
48+
49+ }
50+ }
Original file line number Diff line number Diff line change 1+ package javaStudy .basic .callBack .demo ;
2+
3+ public class BottomService {
4+
5+ public String bottom (String param ) {
6+
7+ try { // 模拟底层处理耗时,上层服务需要等待
8+
9+ Thread .sleep (3000 );
10+
11+ } catch (InterruptedException e ) {
12+
13+ e .printStackTrace ();
14+
15+ }
16+
17+ return param +" BottomService.bottom() execute -->" ;
18+
19+ }
20+
21+ }
Original file line number Diff line number Diff line change 1+ package javaStudy .basic .callBack .demo ;
2+
3+ import java .util .Date ;
4+
5+ public class Test {
6+
7+ public static void main (String [] args ) {
8+
9+ BottomService bottomService = new BottomService ();
10+
11+ UpperService upperService = new UpperServiceImpl (bottomService );
12+
13+ System .out .println ("=============== callBottomService start ==================:" + new Date ());
14+
15+ String result = upperService .callBottomService ("callBottomService start --> " );
16+
17+ //upperTaskAfterCallBottomService执行必须等待callBottomService()调用BottomService.bottom()方法返回后才能够执行
18+
19+ upperService .upperTaskAfterCallBottomService (result );
20+
21+ System .out .println ("=============== callBottomService end ====================:" + new Date ());
22+
23+ }
24+
25+ }
Original file line number Diff line number Diff line change 1+ package javaStudy .basic .callBack .demo ;
2+
3+ public interface UpperService {
4+
5+ public void upperTaskAfterCallBottomService (String upperParam );
6+
7+ public String callBottomService (final String param );
8+
9+ }
Original file line number Diff line number Diff line change 1+ package javaStudy .basic .callBack .demo ;
2+
3+ public class UpperServiceImpl implements UpperService {
4+
5+ private BottomService bottomService ;
6+
7+ @ Override
8+
9+ public void upperTaskAfterCallBottomService (String upperParam ) {
10+
11+ System .out .println (upperParam + " upperTaskAfterCallBottomService() execute." );
12+
13+ }
14+
15+ public UpperServiceImpl (BottomService bottomService ) {
16+
17+ this .bottomService = bottomService ;
18+
19+ }
20+
21+ @ Override
22+
23+ public String callBottomService (final String param ) {
24+
25+ return bottomService .bottom (param + " callBottomService.bottom() execute --> " );
26+
27+ }
28+
29+ }
You can’t perform that action at this time.
0 commit comments