Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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_Pool - Fatal编程技术网

Java 为什么线程在另一个线程开始执行时停止?

Java 为什么线程在另一个线程开始执行时停止?,java,multithreading,pool,Java,Multithreading,Pool,我是Java并发API新手,我已经搜索过了,但没有找到我的问题的答案 我有一个代码,可以查找目录及其子目录中的每个文件,还有一个代码可以复制找到的每个符合指定模式的文件 我将这些代码分为一个名为DirSearch的可运行实现和一个名为FileSearch的可调用实现,并使用ExecutorService提交它们 这就是代码: private boolean execute() { ExecutorService executor = Executors.newFixedThreadPoo

我是Java并发API新手,我已经搜索过了,但没有找到我的问题的答案

我有一个代码,可以查找目录及其子目录中的每个文件,还有一个代码可以复制找到的每个符合指定模式的文件

我将这些代码分为一个名为DirSearch的可运行实现和一个名为FileSearch的可调用实现,并使用ExecutorService提交它们

这就是代码:

private boolean execute() {
    ExecutorService executor = Executors.newFixedThreadPool(threadsNumber);
    BlockingQueue<File> dirQueue = new LinkedBlockingQueue<>();
    BlockingQueue<File> fileQueue = new LinkedBlockingQueue<>(10000);

    boolean isFinished = false;

    try {
        for(int i = 0; i < dirThreads; i++) {
            executor.submit(new DirSearch(dirQueue, fileQueue, count, dirThreads);
        }

        count.incrementAndGet();
        dirQueue.add(baseDir);

        Future<Boolean> future = executor.submit(new FileSearch(filequeue, outputDirectory, filename));

        isFinished = future.get();
    } catch(ExecutionException | InterruptedException | RuntimeException ex) {
        ex.printStackTrace();
    } finally {
        executor.shutdownNow();
    }

    return isFinished;
}

...

private void copyFile(File in, File out) {
    Path inPath = Paths.get(in.getAbsolutePath());
    Path outPath = Paths.get(out.getAbsolutePath(), in.getName());

    try {
        main.updateCurrentLabel(outPath.toString());

        switch(mode) {
            case "1":
                Files.copy(inPath, outPath, StandardCopyOption.REPLACE_EXISTING);
                break;
            case "2":
                Files.move(inPath, outPath, StandardCopyOption.REPLACE_EXISTING);
                break;
            default:
                break;
        }

        main.updateCopiedLabel(String.valueOf(countCpFiles.incrementAndGet()));
    } catch(IOException ex) {
        ex.printStackTrace();
    }
}

...

private class DirSearch implements Runnable {

    ...

    @Override
    public void run() {
        try {
            File dir = dirQueue.take();

            while(dir != new File("")) {
                File[] elements = dir.listFiles();

                if(elements != null) {
                    for(File element : elements) {
                        if(element.isDirectory()) {
                            count.incrementAndGet();
                            dirQueue.put(element);
                        } else {
                            fileQueue.put(element);
                        }
                    }
                }

                if(count.decrementAndGet() == 0) {
                    end();
                }

                dir = dirQueue.take();
            }
        } catch(InterruptedException ex) {
            ex.printStackTrace();
        }
    }

    ...

}

...

private class FileSearch implements Callable<Boolean> {

    ...

    @Override
    public Boolean call() {
        boolean isFinished = false;

        try {
            File file = fileQueue.take();

            while(file != new File("")) {
                incrementAnalyzed();
                String foundFile = file.getName().toLowerCase();

                if(foundFile.matches(filename.replace("?", ".?").replace("*", ".*?"))) {
                    copyFile(file, outputDirectory);
                }

                file = fileQueue.take();
            }

            isFinished = true;
        } catch(InterruptedException ex) {
            ex.printStackTrace();
        }

        return isFinished;
    }
}
private boolean execute(){
ExecutorService executor=Executors.newFixedThreadPool(threadsNumber);
BlockingQueue dirQueue=新建LinkedBlockingQueue();
BlockingQueue fileQueue=新的LinkedBlockingQueue(10000);
布尔值isFinished=false;
试一试{
对于(int i=0;i

问题是:当FileSearch开始复制文件时,其他线程(DirSearch)在复制完成之前,停止并不要查找任何新文件。为什么会发生这种情况?我是否做错了什么,或者这不是正确的方法?

我想到了两个可能的答案,我不能保证它们适用于您的具体情况: 1.Java VM只能从您的CPU获得一个内核,这意味着它一次只能运行一个线程。 2.两个线程都使用相同的变量,这意味着一次只允许一个线程真正操作它。对于这个特定问题,请查找java关键字“synchronized”。
我想问题的根源往往是#1

我想到的两个可能的答案,我不能保证它们适用于你的具体情况: 1.Java VM只能从您的CPU获得一个内核,这意味着它一次只能运行一个线程。 2.两个线程都使用相同的变量,这意味着一次只允许一个线程真正操作它。对于这个特定问题,请查找java关键字“synchronized”。
我想问题的根源往往是#1

dirThreads和threadsNumber的值是多少?请注意
不会编译。@并且Turner threadsNumber是用户的输入,dirThreads是threadsNumber-1。我手工编写了整个程序,因为它不在同一台计算机中,我的错误是dir!=“”。检查编辑的问题。
dirThreads
threadsNumber
的值是什么?注意
dir!=“”
将不会编译。@并且返回threadsNumber是用户的输入,而dirThreads是threadsNumber-1。我手工编写了整个过程,因为它不在同一台计算机中,这是我的错误,dir!=“”。检查编辑的问题。