java - The process cannot access the file because it is being used by another process

后端 未结 4 1355
生来不讨喜
生来不讨喜 2021-02-14 00:24

I have a piece of code that monitors a directory for addition of files. Whenever a new file is added to the directory, the contents of the file are picked and published on kafka

4条回答
  •  清酒与你
    2021-02-14 01:07

    Looking at your code it seems when one file is picked by thread for publishing again another thread is picking it up for publishing. That's why no one is able to delete it. It must be concurrency issue only. You should redesign code based up on criterion : steps which can be run concurrently and those which cannot be. So steps in the entire process are :

    1. pick up a file (main thread should do it)
    2. publish a file (call other thread to do it )
    3. delete the file (called thread should delete it)
    4. check if any file present (again main thread can do it)

    Also the moment a file is selected, you can read it into buffer , delete it and then continue with publish. This will make sure that main thread does not assign this file to some other thread.

提交回复
热议问题