An analogy,
Your teacher makes a homework assignment due at a certain time.
The day of class comes and she asks everyone to turnInTheirWork().
So the students start to turnInTheirWork(), forming a little loop like so…
for(int i = numStudents; i > 1; --i) {
turnInTheirWork(assignment);
}
What does the teacher do?
If she blocked and fully did the work that would mean that she would have to…
// Receive the work
takeAssignment(assignment);
// Grade it on the spot
grade(assignment);
// Mark the grade in the gradebook
record(assignment);
and hand it back to the student.
return assignment;
So the loop would look like so…
for(int i = numStudents; i > 1; --i) {
// BEGIN INSIDE OF turnInTheirWork
// Receive the work
takeAssignment(assignment);
// Grade it on the spot
grade(assignment);
// Mark the grade in the gradebook
record(assignment);
//and hand it back to the student.
return assignment;
// END INSIDE OF turnInTheirWork
}
But wait what if it takes the teacher 5 minutes to grade each assignment? 30 students x 5 minutes is two and a half hours.
Sure the teacher HAS to spend that much time on the work anyway, but now you have a line of students waiting aswell for this work to be done. They could be doing “more productive” things.
So instead let’s tweak this a bit.
Instead of doing ALL the work immediately, the teacher will just take the assignments and put them into a pile. She’ll later get around to actually grading the work and students can check back with her at a later time to get the status of their assignment and/or receive their grade if theirs has been graded.
TO BE CLEAR HERE, the teacher still has to do the work. This means that another computer, thread, task, process, etc. must regularly check the pile for work and when work is present begin processing said work in the background (relative to the program we’re running with the students all queued up).
So lets say it takes all of 2 seconds to just hand in an assignment. 2s x 30 = 1 minute.
So the students only need to wait in line at most one minute vs 2.5 hours thanks to a non-blocking or asynchronous model.
Believe me this is exactly how computers work. The times are usually in uS and mS but the same kind of 1m vs 2.5h savings does happen when you architect non-trivial programs in better ways.
For a computer things that normally take a while are disk I/O, reaching out to the network or kicking off a large loop.
A slightly more complex example would be what my calculus dept. did to grade our finals. All the students handed in their work to one large pile and more than one teacher graded the work. This is a form of parallel processing as all three teachers worked on the grading at the same time. As you can imagine this not only feed the students of having to wait, but it also had the effect of getting the work done much faster. Instead of the students having to ultimately go without their grades for 2.5 hours if there were 5 teachers to grade work they’d only have to wait 30 minutes.
Now say there are 1,500 teachers. We call that a GPU and it grades very quickly.
-Cody