-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallBackBase.java
More file actions
43 lines (36 loc) · 1.07 KB
/
CallBackBase.java
File metadata and controls
43 lines (36 loc) · 1.07 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
package example.CallBackExample;
import java.lang.reflect.Method;
/**
* Callback function
*
* TODO: need to check this example.
*
* Reference:
* - https://zhuanlan.zhihu.com/p/348747542
*/
public class CallBackBase {
public static void main(String[] args) {
Request request = new Request();
System.out.println("[Main] open a thread to async request.");
new Thread(() -> {
try {
request.send(Callback.class, Callback.class.getMethod("processResponse"));
} catch (Exception e) {
e.printStackTrace();
}
}).start();
System.out.println("[Main] done, I will do another thing.");
}
}
class Request {
public void send(Class clazz, Method method) throws Exception {
Thread.sleep(3000);
System.out.println("received the request.");
method.invoke(clazz.newInstance());
}
}
class Callback {
public void processResponse() {
System.out.println("call back: deal with the response.");
}
}