Using locks to protect shared resources
While it's great that our application no longer freezes up during slow file uploads, it raises a potential problem. Suppose a user tries to start a second REST upload while the first is ongoing? Go ahead and try this; launch the sample HTTP server and the application, and try to launch two REST uploads in quick succession, so that the second begins before the first finishes. Note the output from the REST server; depending on your timing, you may see confusing log messages with percentages going up and down as both threads upload files at the same time.
Of course, our sample REST server only simulates a slow link with sleep()
; the actual file upload happens so fast it's unlikely to cause a problem. In a situation with a genuinely slow network, concurrent uploads could be more problematic. While it's possible that the receiving server is robust enough to sensibly handle two threads trying to upload the same file, it's best...