线程完成后,文本不会被打印

时间:2014-03-07 11:17:03

标签: java multithreading netbeans executorservice blockingqueue

请查看以下代码。

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中打印文本“已完成的工作”。但相反,它没有得到印刷。为什么是这样?如何识别所有线程都已完成并需要打印?

2 个答案:

答案 0 :(得分:0)

shutdown()仅指示ES要关机,需要

awaitTermination(长超时,TimeUnit单位)

打印消息之前

答案 1 :(得分:0)

使用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.");

我已经定义了一个实现TestRun的{​​{1}}类,此处未显示。 Runnable对象在其他场景中更有意义。