0

Please have a look at the following code.

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();
        }
    }
}

I want to print the text "completed work" in BigFileWholeProcessor once all the thread work is done. But instead, it is not getting printed. Why is this? How to identify that all the threads are done and need printing?

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • @Smutje: I do not understand how to map those answers into mine. – PeakGen Mar 07 '14 at 11:39
  • You have an executor service and you want to wait for all executed threads to finish. Just check the first answer. – Smutje Mar 07 '14 at 11:42
  • @Smutje: I did. I have never worked with this executer thing, I do not understand how to apply that to here. Do you min providing the solution? – PeakGen Mar 07 '14 at 11:51
  • After your `shutdown` paste try { taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { } – Smutje Mar 07 '14 at 11:55
  • @Smutje: I guess you should provide it as an answer if you think it will help. – PeakGen Mar 07 '14 at 11:56

2 Answers2

0

shutdown() only signal ES to shutdown, you need

awaitTermination(long timeout, TimeUnit unit)

before print message

ben
  • 168
  • 6
0

Use submit() method instead of execute(). The get() method can be used if you want to wait for the thread to finish at any point of time. Read documentation on use of Future object for further details.

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

I have defined a TestRun class implementing Runnable, not shown here. The Future object makes more sense in other scenarios.

Sourabh Bhat
  • 1,793
  • 16
  • 21