[introduction to Java] callback in day4 Java

After another busy week, things have almost been solved. Finally, I can continue to write my blog (you've been waiting for a long time).

This time, let's talk about a very interesting thing in Java - callback.

What is a callback? Seriously speaking, in computer programming, a callback function refers to a reference to a piece of executable code passed to other code through function parameters. This design allows the underlying code to call subroutines defined at a higher level.

Don't worry, don't worry, and listen to me slowly.

Take chestnuts for example. Set up a situation where the boss arranges the employee to do things, then asks him to finish and call him. Of course, the boss will not wait for the employees to finish their work before doing other things. Instead, he will only be busy with his own work after giving an account of the task.

This example contains the idea of asynchrony + callback. When an employee reports this process to the boss after completing a task, it is called callback. Of course, if the report is reported, the boss must first agree with the employee on the reporting method, such as e-mail, telephone, etc., and the reporting method is to register the callback function. The callback function here must comply with the specification of the interface.

I still don't seem to understand? Let's get the code.

First define an interface:

Define a boss class to implement the interface for receiving reports:

Define a worker interface:

Define an employee class.

Then test:

Test results:

At this point, the interaction between the employee and the boss is completed, which is a simple synchronous callback. The boss can arrange work for employees through the worker interface without caring about which employee is working. The worker reports work to the boss through receivereport. The two classes interact through callback through the interface, which can be well decoupled, because the boss can arrange different employees as long as they implement the worker interface, Employees can also report to different bosses as long as the receivereport interface is implemented.

In fact, the core idea of callback is to pass its own this pointer to the caller. Just like here, the employee is passed into the boss class, and the callback is registered in the work method, so the interaction between the two is very strong.

So why use callbacks? If the boss wants to register some employee information, such as name, before the employee completes his work, with the callback mechanism, he can do whatever he wants inside the boss by passing in the this pointer, without designing new methods to obtain it. Moreover, the more data he needs to obtain, the more obvious the advantage of callback.

In fact, it's just a simple one-to-one relationship. If it's a boss and multiple employees, it's a simple observer model. If it's multiple bosses and multiple employees, it's a simple producer consumer model.

Of course, this is just a simple synchronous callback. Employees can only complete tasks one by one, that is, the former employee must wait for the latter employee to complete the task before starting the task. In fact, employees generally work at the same time.

In another scenario, there are ten employees, the boss publishes the task, and the top three people who complete the task are rewarded with a bonus. Therefore, asynchronous callback is required. Just use threads when sendtask. Let's modify the code:

The lock is not used here because the set time interval is 0-10s, the probability of concurrent conflict is very low, and because there is no multi-threaded content, it is not used for the time being. Just know that in the sendtask method, threads are started successively to call the work method of each worker. After the thread is started, it will execute at the same time. After execution, it will call the receivereport method of the boss to feed back the results to the boss. After receiving the results, it will call the getreward method of the worker to pay bonuses to the top three according to the completion order. In fact, this is a two-way callback. The boss passes this pointer to the worker, and the worker passes his own this pointer to the worker.

The program execution results are as follows:

Because multithreading is used, the results of each run may be different. If different results are obtained, it is a very normal phenomenon.

Given these two chestnuts, I should have a certain understanding of callback.

In fact, callback is just an idea, not a unique content in Java. This kind of idea appears to solve specific problems in a specific scenario. It has its value only when it is correctly applied, rather than using it in order to use it.

So far, the callback explanation is completed. If there is any error in the explanation, you are welcome to criticize and correct. We also welcome your continued attention.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>