So... I'm trying to understand this for a moment at a more fundamental level. Likely more fundamental than I need... but meh...
Uh, I'm a C# developer who's kind of been stuck in a synchronous world and really trying to understand some fundamentals to the asynchronous world... That being said...
Something like JavaScript and there by Node JS. Runs on a single thread. I get that.
But... and please do correct me here... By use of callbacks, Javascript can run work asynchronously. So...
Please don't hate me for this horrible example, just to give an idea... But trying to make sure the general concept is presented.
Via callbacks and asynchronous programming, I could do something like..
file1.txt = 500mb file size of text.
file2.txt = 1kb file size of text.
file3.txt = 1kb file size of text.
file4.txt = 1kb file size of text.
function myFileReaderToConsoleCallBack(fileName) {
.../ reading the file and writing the file to console.log() /..
}
function myActualFunctionCalled(callback, fileName){
callback(fileName);
}
myActualFunctionCalled(myFileReaderToConsoleCallBack, "file1.txt");
myActualFunctionCalled(myFileReaderToConsoleCallBack, "file2.txt");
myActualFunctionCalled(myFileReaderToConsoleCallBack, "file3.txt");
myActualFunctionCalled(myFileReaderToConsoleCallBack, "file4.txt");
Long story short, file1 should take a while to flush to console. files 2-4 should be super fast.
In doing the above, because the file reading and printing out to console is being done through a callback, I'll end up getting files 2, 3, and 4, mixed in with file1.txt printing out to console. But they'll all process at the same time right?
How does that even work on a single thread? Is there some context switching on the thread going on or am I just completely off my rocker and missing the whole boat on all of this entirely?
The main thing that's getting me here is that the read and flushing the contents of file1.txt wouldn't really ever have a break in the execution of the thread. So... How would files 2-4 be able to process if file1.txt is still flushing out to console?