且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java-进程无法访问文件,因为该文件正在被另一个进程使用

更新时间:2021-12-21 23:14:28

看着您的代码,似乎线程选择了一个文件再次发布,而另一个线程选择了它进行发布.这就是为什么没人能删除它的原因. 它必须仅是并发问题.您应该根据以下准则重新设计代码:可以同时运行的步骤和不能同时运行的步骤. 因此,整个过程中的步骤如下:

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. 拾取文件(主线程应该这样做)
  2. 发布文件(调用其他线程进行操作)
  3. 删除文件(称为线程应将其删除)
  4. 检查是否存在任何文件(再次主线程可以做到)

此外,选择文件后,您可以将其读入缓冲区,将其删除,然后继续进行发布.这样可以确保主线程不会将此文件分配给其他线程.

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.