How to use mkdirs in a thread safe manner in Java?

前端 未结 5 2265
执念已碎
执念已碎 2021-02-14 17:51

After experiencing issues with mkdirs() and poking around the interwebs, I get the impression that there are thread safety issues with mkdirs().

Is there a way to ensure

5条回答
  •  走了就别回头了
    2021-02-14 18:41

    Do all your directory creation in a worker thread that serializes everything. You can use a Looper and a Handler to make it easy to post Runnables that call mkdirs to your worker thread. When you're done making directories, you can call Looper.quit() to end the thread after it processes the last posted Runnable. The documentation for Looper has sample code that shows how near to trivial this is to do.

提交回复
热议问题