Search code examples
javadiskspace

Virtual disk space reservation in java


Assume a multithreaded application scenario, in which every thread acquires some data (one or more files) from the network, performs some processing and then saves the results on the hard disk of the hosting machine.

In such a scenario, there is always the possibility that the disk space is exhausted, leading to unexpected service behavior (e.g., a system crash).

To avoid a case like that, it would be helpful if Java provided a means of reserving hard disk space, but, as verified in an earlier question, such an option is not available and even if it were, it could lead to inefficient allocation (e.g., in the case of a decompressing application, which does not know beforehand the total size of the decompressed data).

So, an alternative could be to make "virtual disk space reservations", e.g. by keeping in memory a static registry of the free space and having each thread request capacity from the registry before proceeding.

Are there any better alternatives, or improvements to this approach?

Is there any (preferably open source) Java library that implements such functionality?


Solution

  • An abstract way to implement this might be to use a constant or user inputted value of how much disk space the multi-threaded application is allowed to use, save this as variable and have synchronized get and set methods that will get the value of allowed space, allocate it space for the thread (as much is needed but not more then available), and then minus that from the total so other threads may see a decreased 'disk space' and once a thread has finished and data is deleted re-add the value so the used 'disk space' becomes usable to other threads?

    EDIT:

    it could lead to inefficient allocation (e.g., in the case of a decompressing application, which does not know beforehand the total size of the decompressed data).

    If this occurs and the thread 'sees' (through a constant check while extracting file) that it has reached its limit of 'disk space', it could then request for more space allocation if available, or be put back into a queue until the needed space has been free'd up by other threads