What are the applications for queues?
Let's first look at the computer composition level, first of all, the CPU resource competition. In a computer system with multiple terminals, multiple users need to use the CPU to run the program separately, and the operating system will queue each request in the order of time, plus requests. Each time the CPU is assigned to the task of the team head, it is finished to make it out of the team, then sequentially.
The second example is the problem of the speed mismatch between the host and the external device. Let's take the printer and the mainframe as an example. Host output data to the printer printing, the host output data is much faster than the printer printing speed, so there is a print data buffer, the main data is written to buffer, write full after the host on the pause output, and then to do other time, only to the printer to complete the printing, again request the host to send data, the host will continue to output data. This utilization of the queue can improve the efficiency of the host.
I'm here to pick a common software-level example, the bank queue problem. First talk about the realization of ideas, the first customer arrives at the moment of 0, after each customer arrives at the time of the previous customer to set a random value, so when the customer arrives need to produce two random number, one is the customer handles the business time Durtime, one is the next customer arrives time interval intertime, Assuming the current time is Occurtime, the next customer arrives at Occurtime+intertime.
Customers who have just arrived should be inserted into the queue that currently contains the least element.
In JavaScript usage, queues are typically used to sort tasks. Task queue tasks are deferred execution (resolution of State consistency) in the order in which they are queued, that is, after the current task is completed, the subsequent task is executed, and the queued task executes immediately if no task is currently in place. The code is as follows:
Application of the JavaScript queue