【问题标题】:Text is not getting printed once the Threads are done [duplicate]线程完成后不会打印文本[重复]
【发布时间】:2014-04-10 12:20:21
【问题描述】:

请看下面的代码。

public class BigFileWholeProcessor {
    private static final int NUMBER_OF_THREADS = 2;
    public void processFile(String fileName) {

        BlockingQueue<String> fileContent = new LinkedBlockingQueue<String>();
        BigFileReader bigFileReader = new BigFileReader(fileName, fileContent);
        BigFileProcessor bigFileProcessor = new BigFileProcessor(fileContent);
        ExecutorService es = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
        es.execute(bigFileReader);
        es.execute(bigFileProcessor);
        es.shutdown();

        if(es.isTerminated())
        {
            System.out.println("Completed Work");
        }

    }
}



public class BigFileReader implements Runnable {
    private final String fileName;
    int a = 0;
    public static final String SENTINEL = "SENTINEL";

    private final BlockingQueue<String> linesRead;
    public BigFileReader(String fileName, BlockingQueue<String> linesRead) {
        this.fileName = fileName;
        this.linesRead = linesRead;
    }
    @Override
    public void run() {
        try {
            //since it is a sample, I avoid the manage of how many lines you have read
            //and that stuff, but it should not be complicated to accomplish
            BufferedReader br = new BufferedReader(new FileReader(new File("E:/Amazon HashFile/Hash.txt")));
            String str = "";

            while((str=br.readLine())!=null)
            {
                linesRead.put(str);
                System.out.println(a);
                a++;
            }
            linesRead.put(SENTINEL);

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        System.out.println("Completed");
    }
}



public class BigFileProcessor implements Runnable {
    private final BlockingQueue<String> linesToProcess;
    public BigFileProcessor (BlockingQueue<String> linesToProcess) {
        this.linesToProcess = linesToProcess;
    }
    @Override
    public void run() {
        String line = "";
        try {
            while ( (line = linesToProcess.take()) != null) {
                //do what you want/need to process this line...

                if(line==BigFileReader.SENTINEL)
                {
                    break;
                }
                String [] pieces = line.split("(...)/g");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

一旦所有线程工作完成,我想在BigFileWholeProcessor 中打印文本“完成的工作”。但相反,它没有被打印出来。为什么是这样?如何识别所有线程都已完成并需要打印?

【问题讨论】:

  • @Smutje:我不明白如何将这些答案映射到我的。
  • 你有一个执行器服务,你想等待所有执行的线程完成。只需检查第一个答案。
  • @Smutje:我做到了。我从来没有使用过这个执行器的东西,我不明白如何将它应用到这里。您是否愿意提供解决方案?
  • 在你的shutdown 粘贴之后尝试 { taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } 捕捉 (InterruptedException e) { }
  • @Smutje:我想如果您认为它会有所帮助,您应该提供它作为答案。

标签: java multithreading netbeans executorservice blockingqueue


【解决方案1】:

使用submit() 方法而不是execute()。如果您想在任何时间等待线程完成,可以使用get() 方法。阅读有关使用 Future 对象的文档以了解更多详细信息。

ExecutorService es = Executors.newFixedThreadPool(2);
Future<?> f = es.submit(new Thread(new TestRun()));
f.get(); // Wait for result... (i.e similar to `join()` in this case)
es.shutdown(); // Shutdown ExecutorService 
System.out.println("Done.");

我定义了一个实现RunnableTestRun 类,此处未显示。 Future 对象在其他情况下更有意义。

【讨论】:

    【解决方案2】:

    shutdown() 仅向 ES 发出关闭信号,你需要

    awaitTermination(长时间超时,TimeUnit单位)

    打印前消息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 2016-03-16
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多