【发布时间】:2017-02-06 14:44:16
【问题描述】:
我的情况是,Windows 文件夹上的文件移动/创建正在为使用该路径上的文件继续执行的任务造成时间问题
所以我正在使用 thread.sleep(30ms) 让运行该任务的守护线程休眠,直到 Windows 可以正常工作并允许我的任务在没有任何 FileNotFoundException 的情况下正常运行。
初始化类
Thread t = new Thread (processTask);
t.setdaemon(true)
t.start();
任务类
class ProcessTask() extends Runnable
{
Files.move(source, destination, copyoption);
Thread.sleep(30ms); //to wait for windows to complete move
new FileInputstream(sourceFile);
}
thread.sleep(30ms) 肯定只会让我当前的守护线程睡眠 吗?有没有办法确认睡眠在哪里起作用
【问题讨论】:
-
您的代码自相矛盾。如果你从
source移动到destination,那么之后你应该很明显不能打开源代码,但是,当然,sourceFile是一个完全不参与操作的变量。此外,FileInputstream使用的库代码与 NIO 不同。假设destination是Path代表您实际想要打开的内容,我会尝试使用Files.newInputStream(destination)。
标签: java multithreading threadpool daemon thread-sleep