简体   繁体   中英

Effect of Thread.sleep on a runnable task waiting on windows to complete file creation/moving

I have a condition where file moving/creation on windows folder is creating a timing problem for a task to proceed using file on that path

So I am sleeping the deamon thread running that task using thread.sleep(30ms) until windows does it work and allows my task to run fine without any FileNotFoundException.

Initialization Class

 Thread t = new Thread (processTask);
 t.setdaemon(true)
 t.start();

Task class

 class ProcessTask() extends Runnable 
  {
   Files.move(source, destination, copyoption);
   Thread.sleep(30ms); //to wait for windows to complete move
   new FileInputstream(sourceFile);
  }

Would thread.sleep(30ms) for sure sleep only my current deamon thread ? Would there be a way to confirm where sleep is acting

Thread.sleep() affects the current thread. It doesn't put other threads to sleep.

However using Thread.sleep() is never a good idea to use for program logic. Especially when waiting for some other action to finish. Those should be handled with notification mechanisms, not waiting random amounts and hope that everything will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM