1. Callback function classification
Callback function distinction: synchronous callback and asynchronous callback
Synchronous callback: the meaning is only to complete the method call;
Asynchronous call: concurrency can be realized, and the main business thread can be released in time; asynchronous thread completes the work, executes callback function, and completes the aftermath work; the execution efficiency is improved.
2. Code examples
1. Note testing
package com.callback2;
public class AsyncCallBack {
public static void main(String[] args) {
System.out.println("Start of the main business thread ID:" + Thread.currentThread().getId());
System.out.println("------------------");
Son son = new Son();
Mother mother = new Mother(son);
mother.notice();
son.writeHomeWork();
System.out.println("End of the main business thread ID:" + Thread.currentThread().getId()+"\n");
}
}
2. Mother
package com.callback2;
public class Mother {
private Son son;
public Mother(Son son) {
this.son = son;
}
public void notice() {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Notify the mother of the thread ID." + Thread.currentThread().getId());
cookFood("Bread");
}
}).start();
}
public void cookFood(String bread) {
System.out.println("Current cooking thread ID." + Thread.currentThread().getId());
try {
System.out.println("Mother Baking" + bread + "...");
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Mother has baked the bread");
String message = "Ming, come and eat!";
son.callback(message);
}
}
3. My son Xiao Ming
package com.callback2;
public class Son {
private String status = "";
public void writeHomeWork() {
System.out.println("Xiaoming writing homework thread ID: " + Thread.currentThread().getId());
System.err.println("Ming is writing his homework...") ;
setStatus("Writing homework");
}
public void callback(String message) {
System.out.println("Callback Xiaoming meal thread ID: " + Thread.currentThread().getId());
System.err.println(message);
System.err.println("Okay, coming right up!") ;
System.out.println("Ming started eating!") ;
setStatus("Eating in progress");
System.out.println("Xiaoming executing meal thread ID." + Thread.currentThread().getId());
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
4. Implementation results
Business Main Thread Start ID: 1
------------------
Xiaoming writing homework thread ID: 1
Xiaoming writing homework in progress...
Business main thread end ID:1
Notify mother thread ID:12
Currently cooking thread ID:12
Mother baking bread...
Mother baked the bread
Callback to Xiaoming to eat Thread ID: 12
Ming, come and eat!
Okay, coming right up!
Ming starts eating!
Xiaoming executes meal thread ID: 12
5. Result analysis
1) Xiao Ming uses the main thread to do his homework (if he can’t finish it, he doesn’t need to care);
2) Mother uses the new thread to cook [concurrent], mother finishes the meal, executes the callback function, and notifies Xiao Ming to eat;
3) Xiao Ming uses a new thread and starts to eat.
Read More:
- Java callback function implementation case
- After asynchronous file import and springboot multipartfile upload, the @async asynchronous processing reports an error: nosuchfileexception
- Asynchronous processing of HTTP request by Java_ Method 2: through deferredresult
- Asynchronous processing of HTTP request by Java_ Method 1: through callable
- Spring boot uses thread pool to realize asynchronous processing without return and asynchronous processing with return
- [Solved] Jenkins error: Asynchronous execution failure
- Code case of XXL job executor
- Problems and causes of Java’s main function format (public static void main (string args()))
- C++ String case conversion and transform, tower, upper, usage
- [Solved] port (127.0.0.1:64444): java.net.SocketException “Interrupted function call: accept failed“
- mybatis “case when” Error: Failed to process, please exclude the tableName or statementId.
- [Solved] In case of “transactionmanager” while setting bean property “transactionmanager” error
- [Solved] Hibernate Error: java.lang.StackOverflowError at java.lang.Integer.toString(Integer.java:402)
- Java uses class array to report error Exception in thread “main” java.lang.NullPointerException solution
- Java error: java.lang.NoSuchMethodError
- [Solved] Java 9 reflection error: java.lang.reflect.InaccessibleObjectException
- The java springboot websocket service server actively closes the connection and causes java.io.EOFException to be thrown
- [Solved] Hbase-shell 2.x Error: Unhandled Java exception: java.lang.IncompatibleClassChangeError: Found class jline.Terminal…
- [Solved] JAVA OpenCV Startup Error: java.lang.UnsatisfiedLinkError
- Java learning unreported exception java.io.IOException ; must be caught or declared to be thrown