CallbackFunction, as the name suggests, is a function used for callback. The callback function is just a functional fragment, a function implemented by the user according to the callback function calling convention. The callback function is part of a workflow, and the workflow determines the timing of the function call (callback).
Callbacks should originally be a very simple concept, but maybe because we only use the interface of callbacks written by the system for us, we rarely implement callbacks, so we implement callbacks ourselves. I was still a little bit dizzy when I started, so I am writing this article to record it, and also share with you how to write a callback interface.
Callback
The concept of callback: For example, if we want to ask someone else a question, we tell the question to the other person After talking for a while, the other party said yes, when I finish this question, I will tell you that callback is used at this time, because we don't know when the other party will finish it, but the other party will take the initiative to find us after finishing it.
Synchronous callback
When the code runs to a certain location, if it encounters code that requires a callback, it will wait here and wait for the callback result to return before continuing to execute. .
Asynchronous callback
When the code is executed to the code that requires callback, it will not stop, but will continue to execute. Of course, the result of the callback may be changed after a while. Return back.
Specific code:
The overall code is still very simple. It simulates a printer and a person. The printer has the function of printing, but printing takes time. Feedback cannot be given while receiving the task, and you need to wait for a period of time before feedback can be given. All this person wants to do is print a resume and know the results of the printout. The code here implements both methods.
Callback.java
1 2 3 |
|
Printer.java
1 2 3 4 5 6 7 8 9 10 11 |
|
People.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The above is the detailed content of Java callback principle implementation code sharing (picture). For more information, please follow other related articles on the PHP Chinese website!