Java calls and callbacks summary (2)

Java calls and callbacks summary (2)


The type of call

There are three kinds of calls, an ordinary call (synchronous call), asynchronous calls , asynchronous callback .

Three kinds of calling features

Ordinary call : also known as synchronous call , most commonly called, can cause obstruction .
img

Asynchronous call : asynchronous call, to solve the problem synchronous call blocking , but did not return results .
img

Asynchronous callback : asynchronous callback, to solve the blockage , can also return results .
img

Three progressive relationship from weak to strong as 普通调用< 异步调用 < 异步回调the most important differences between the three in fact only two points, 1: This call is not a cause of blocking the main thread , 2: when we call, can you return implementation of the results . it is clear that ordinary call, will cause obstruction, but after the implementation is complete, we can get the results immediately on the execution! However, because there are a lot of task execution time it is very long, so we will block the main thread task, led asynchronous calls occur, asynchronous call and synchronous call difference is, synchronous call blocking, but you can get the result of the execution, asynchronous call does not block, but can not know the result of this execution ! so how to solve not have to known problem task execution results? that is required at the time of execution, after the execution is over, directly notify the main thread, notify the main thread, that is to use object class main thread is located, and then modify the field representing the results or property, or execute a method to let the outside world know, it means the result of the implementation of the outside world has been learned.

Synchronous and asynchronous how to solve?

Synchronization will block the main thread, because the process we performed linear, linear because there is no other threads of execution, only one, because the same time only one task to perform, is exclusive, so the task can only be blocked, such as the task execution is over in order to perform another task!
asynchronous it? asynchronously without blocking, because it broke the limit only one thread, so to asynchronous, it is necessary to create multiple threads, then the java inside, creating multiple thread, so you can achieve asynchronous up!
img

Callback how to understand?

Whether synchronous or asynchronous calls, all calls B calls A single line, but this is the case, for example, we call B in the A-threaded, so we can not know the result of the implementation of B, or A waiting a long time to make, to make two tasks to complete. so we have two-way call, rather than a one-way call, this is the case, you can call the two sides, we can know the results, the callback way, typically by Interface Interface to achieve, but can also be Class achieve, but generally is accomplished by Interface, we need-oriented programming interface.


ref:
1. the Java callback function, read to understand , 2. the Java interfaces Detailed callback mechanism , 3. the Java callback mechanism to resolve , 4. asynchronous call understanding , 5. the Java callback mechanism summary

Guess you like

Origin www.cnblogs.com/prayjourney/p/11421698.html