Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:依赖于文件的线程_Java_Multithreading - Fatal编程技术网

Java:依赖于文件的线程

Java:依赖于文件的线程,java,multithreading,Java,Multithreading,我想知道下面的代码出了什么问题,是否有人可以帮助我。我想做的是: 创建包含一些.txt文件的文件夹 为该文件夹中包含的每个.txt文件创建线程 为每个线程执行任务(在这种特殊情况下,只需等待几秒钟) 然后在任务完成后,删除单个线程所依赖的文件 我的问题是,每次我运行代码时,除了在执行任务后删除所有文件之外,一切都很顺利。结果是只有一个.txt文件被删除,其他文件仍在该文件夹中:/ 我做错了什么 任何帮助都会非常好。提前谢谢 朱利安:D public abstract class Thread

我想知道下面的代码出了什么问题,是否有人可以帮助我。我想做的是:

  • 创建包含一些.txt文件的文件夹
  • 为该文件夹中包含的每个.txt文件创建线程
  • 为每个线程执行任务(在这种特殊情况下,只需等待几秒钟)
  • 然后在任务完成后,删除单个线程所依赖的文件
我的问题是,每次我运行代码时,除了在执行任务后删除所有文件之外,一切都很顺利。结果是只有一个.txt文件被删除,其他文件仍在该文件夹中:/

我做错了什么

任何帮助都会非常好。提前谢谢

朱利安:D

public abstract class Threads {
static Formatter x;
public static void main(String[] args) throws IOException {


    //create folder with 10 .txt-files
    File theFolder = new File("C:\\Users\\Gamer\\Desktop\\TheFolder\\");
    theFolder.mkdir();

    for(int files = 0; files <= 10; files++) {
        x = new Formatter("C:\\Users\\Gamer\\Desktop\\TheFolder\\"+files+".txt");
    } 

    System.out.println("Created the folder and files");
    //Close the formatter
    x.close();


    //Start a thread for each file in that folder
    System.out.println(Files.list(Paths.get("C:\\Users\\Gamer\\Desktop\\TheFolder\\")).count());
    Thread[] threads = new Thread[(int) Files.list(Paths.get("C:\\Users\\Gamer\\Desktop\\TheFolder\\")).count()];

    for(int i = 0; i < threads.length; i++) {
        int del = i;
        threads[i] = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(2000);

                    //after timeout of ones thread, terminate file
                    File theFile = new File("C:\\Users\\Gamer\\Desktop\\TheFolder\\"+del+".txt");
                    theFile.delete(); 
                    System.out.println("deleted file: "+del);
                } catch (InterruptedException e) {e.printStackTrace();}
            }
        });
        threads[i].start(); System.out.println("Started thread no. "+i);}       

    System.out.println("end");
    }
}
公共抽象类线程{
静态格式化程序x;
公共静态void main(字符串[]args)引发IOException{
//创建包含10.txt文件的文件夹
File theFolder=新文件(“C:\\Users\\Gamer\\Desktop\\theFolder\”;
theFolder.mkdir();

对于(int files=0;files您只关闭一个格式化程序(最后一个),因此所有其他文件仍有打开的句柄,无法删除。请尝试以下操作:

 for(int files = 0; files <= 10; files++) {
     x = new Formatter("C:\\Users\\Gamer\\Desktop\\TheFolder\\"+files+".txt");
     x.close();
 } 
 //x.close(); <-- this will only close the last formatter that is assigned to "x" after the loop is done

for(int files=0;我假定这些文件名为“C:\\Users\\Gamer\\Desktop\\TheFolder\\0.txt”[etc]…?您可以动态读取文件的名称。是否有任何错误?您是否尝试过单步执行?您是否检查了线程是否为守护进程?在这种情况下,他可能只能删除最后创建的文件?不是吗?哇,谢谢。我没想到解决方案会那么简单。是的,因为所有其他文件句柄都已打开,只能删除最后一个文件。实际上,在检查boolean successful=theFile.delete();,时,您会注意到,除了最后一个文件之外,其他所有文件都是false。