When calling the method of writing to the database, pass in the callback function
When the method of writing to the database is called, another thread will be started to do the actual database writing operation. The method will return immediately after the thread is started.
When the thread that performs the database write operation completes, call the callback function passed in in the first step.
// 定义Callback回调函数接口
function Callback(...);
function writedb(string sql, Callback callback) {
// 创建新的线程来进行DB操作
Thread t = new Thread() {
// 更新DB
...
// 调用回调函数
callback(...);
}
t.start();
return;
}
function main() {
...
writedb(sql, new Callback(...) {
// 回调函数代码
...
});
// 其他代码,将与db操作同时进行
...
}
1. I think it will be clearer to use the producer-consumer model to solve your needs.
2. Asynchronous callbacks do not necessarily require multi-threading. JavaScript on the browser is a perfect example of a single-process, single-thread implementation of asynchronous callbacks.
When calling the method of writing to the database, pass in the callback function
When the method of writing to the database is called, another thread will be started to do the actual database writing operation. The method will return immediately after the thread is started.
When the thread that performs the database write operation completes, call the callback function passed in in the first step.
1. I think it will be clearer to use the producer-consumer model to solve your needs.
2. Asynchronous callbacks do not necessarily require multi-threading. JavaScript on the browser is a perfect example of a single-process, single-thread implementation of asynchronous callbacks.