forked from coderbruis/JavaSourceCodeLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFutureMain.java
More file actions
64 lines (61 loc) · 1.61 KB
/
Copy pathFutureMain.java
File metadata and controls
64 lines (61 loc) · 1.61 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.learnjava.thread.synchronize;
import java.util.ArrayList;
import java.util.List;
/**
* 模拟Netty通讯过程
* 主线程,获取子线程的结果
*
* @author lhy
* @date 2022/2/10
*/
public class FutureMain {
private static List<RequestFuture> reqs = new ArrayList<>();
public static void main(String[] args) {
mockClient();
mockServer();
}
/**
* 模拟服务端 接受结果
*/
private static void mockServer() {
for (RequestFuture req : reqs) {
/**
* 主线程获取结果
*/
Object result = req.get();
System.out.println("服务端接受到响应结果:" + result.toString());
}
}
/**
* 模拟客户端 发送请求
*/
private static void mockClient() {
for (int i = 0; i < 100; i++) {
long id = i;
RequestFuture req = new RequestFuture();
req.setId(id);
req.setRequest("hello world");
/**
* 把请求缓存起来
*/
RequestFuture.addFuture(req);
/**
* 将请求放入到请求列表中
*/
reqs.add(req);
sendMsg(req);
SubThread subThread = new SubThread(req);
/**
* 开启子线程
*/
subThread.start();
}
}
/**
* 模拟请求处理
* @param req
*/
private static void sendMsg(RequestFuture req) {
System.out.println("客户端发送数据,请求id为===============" + req.getId());
}
}