I implemented a download manager, which works fine except that I noted one thing, sometimes the thread blocks for a while(50 milliseconds to up to 10 seconds) when writing to files, I am running this program on Android(Linux based), my guess is if there’re some kind of buffer in the OS level that needs to be flushed, and my writing actually writes to that buffer, and if that buffer is full, writing needs to wait.
My question is what is the possible reason that could cause the blocking?
IO is well known to be a ‘blocking’ activity, hence your question should be ‘what should you do while your program is busy waiting for IO to complete’
Adopting some of the well known concurrency strategy and event-based programming pattern is a good start