Future 模式

future 模式类似于一张提货单。

future 意思就是未来、期货(金融领域)。

实际案例

类信息概览:

类名 说明
Main.java 方法的总入口
FutureData.java 未来的数据
Data.java 数据
Host.java 客户端
RealData.java 实际的数据

定义

  • FutureData.java
  [java]
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
package com.github.houbb.thread.learn.easy.learn.future; /** * 2018/2/3 * * @author houbinbin * @version 1.0 * @since 1.7 */ public class FutureData implements Data { private RealData realData; private boolean isReady = false; public synchronized void setRealData(RealData realData) { if(isReady) { return; //balk } this.realData = realData; this.isReady = true; notifyAll(); } @Override public synchronized String getContent() { while (!isReady) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } return this.realData.getContent(); } }
  • Data.java
  [java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.github.houbb.thread.learn.easy.learn.future; /** * 2018/2/3 * * @author houbinbin * @version 1.0 * @since 1.7 */ public interface Data { String getContent(); }
  • Host.java
  [java]
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
package com.github.houbb.thread.learn.easy.learn.future; /** * 2018/2/3 * * @author houbinbin * @version 1.0 * @since 1.7 */ public class Host { public Data request(final String string) { // System.out.println("Host.request() START..."); //1. future final FutureData futureData = new FutureData(); //2. new Thread(new Runnable() { @Override public void run() { RealData realData = new RealData(string); futureData.setRealData(realData); } }).start(); System.out.println("Host.request() END..."); return futureData; } }
  • RealData.java
  [java]
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
package com.github.houbb.thread.learn.easy.learn.future; import com.github.houbb.thread.learn.easy.learn.ThreadUtil; /** * 2018/2/3 * * @author houbinbin * @version 1.0 * @since 1.7 */ public class RealData implements Data { private final String string; public RealData(String string) { ThreadUtil.sleep(1000); this.string = string+"-real"; } @Override public String getContent() { return this.string; } }

测试

  • Main.java
  [java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.github.houbb.thread.learn.easy.learn.future; /** * 2018/2/3 * * @author houbinbin * @version 1.0 * @since 1.7 */ public class Main { public static void main(String[] args) { Data futureDataA = new Host().request("A"); Data futureDataB = new Host().request("C"); Data futureDataC = new Host().request("D"); System.out.println(futureDataA.getContent()); System.out.println(futureDataB.getContent()); System.out.println(futureDataC.getContent()); } }
  • 测试结果
  [plaintext]
1
2
3
4
5
6
7
8
9
Host.request() START... Host.request() END... Host.request() START... Host.request() END... Host.request() START... Host.request() END... A-real C-real D-real

实现方式

UML & Code

UML

Future

Code

代码地址

future

系列导航

多线程系列导航