Java NIO java.nio.file.DirectoryNotEmptyException if the folder is being open in windows explorer

Java nio directory delete gives java.nio.file.DirectoryNotEmptyException if the directory is open in windows explorer. I am using Java 1.7 and below is the code for DeleteDirVisitor's visitFile and postVisitDirectory methods.

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    Files.delete(file);
    return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
    if(exc == null){
        Files.delete(dir);
        return FileVisitResult.CONTINUE;
    }
    throw exc;
}

Strangely when i debug the delete call then it doesn't throw any exception but when i run then it fails. Seems to be a timing issue. Also, the above API's work 100% if i close the explorer window pointing to the directory being deleted.

Can someone suggest me a work around so that i can get it working 100%.

Thanks,