Mahi G
Mahi G

Reputation: 391

How do we know threadPoolExecutor has finished execution

I have a parent thread that sends messages to MQ and it manages a ThreadPoolExecutor for worker threads which listen to MQ and writes message to output file. I manage a threadpool of size 5. So when I run my program, I have 5 files with messages. Everything works fine until here. I now need to merge these 5 files in my parent thread.

How do I know ThreadPoolExecutor finished processing so I can start merging files.

public class ParentThread {
    private MessageSender messageSender;
    private MessageReciever messageReciever;
    private Queue jmsQueue;
    private Queue jmsReplyQueue;
    ExecutorService exec = Executors.newFixedThreadPool(5);

    public void sendMessages() {
        System.out.println("Sending");
        File xmlFile = new File("c:/filename.txt");
        List<String> lines = null;
        try {
            lines = FileUtils.readLines(xmlFile, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (String line : lines){
            messageSender.sendMessage(line, this.jmsQueue, this.jmsReplyQueue);
        }
        int count = 0;
        while (count < 5) {
            messageSender.sendMessage("STOP", this.jmsQueue, this.jmsReplyQueue);
            count++;
        }

    }

    public void listenMessages() {
        long finishDate = new Date().getTime();
        for (int i = 0; i < 5; i++) {
            Worker worker = new Worker(i, this.messageReciever, this.jmsReplyQueue);
            exec.execute(worker);
        }
        exec.shutdown();

        if(exec.isTerminated()){ //PROBLEM is HERE. Control Never gets here.
            long currenttime = new Date().getTime() - finishDate;
            System.out.println("time taken: "+currenttime);
            mergeFiles();
        }
    }
}

This is my worker class

public class Worker implements Runnable {

    private boolean stop = false;
    private MessageReciever messageReciever;
    private Queue jmsReplyQueue;
    private int processId;
    private int count = 0;

    private String message;
    private File outputFile;
    private FileWriter outputFileWriter;

    public Worker(int processId, MessageReciever messageReciever,
            Queue jmsReplyQueue) {
        this.processId = processId;
        this.messageReciever = messageReciever;
        this.jmsReplyQueue = jmsReplyQueue;
    }

    public void run() {
        openOutputFile();
        listenMessages();
    }


    private void listenMessages() {
        while (!stop) {
            String message = messageReciever.receiveMessage(null,this.jmsReplyQueue);
            count++;
            String s = "message: " + message + " Recieved by: "
                    + processId + " Total recieved: " + count;
            System.out.println(s);
            writeOutputFile(s);
            if (StringUtils.isNotEmpty(message) && message.equals("STOP")) {
                stop = true;
            }
        }
    }

    private void openOutputFile() {
        try {
            outputFile = new File("C:/mahi/Test", "file." + processId);
            outputFileWriter = new FileWriter(outputFile);
        } catch (IOException e) {
            System.out.println("Exception while opening file");
            stop = true;
        }
    }

    private void writeOutputFile(String message) {
        try {
            outputFileWriter.write(message);
            outputFileWriter.flush();
        } catch (IOException e) {
            System.out.println("Exception while writing to file");
            stop = true;
        }
    }
}

How will I know when the ThreadPool has finished processing so I can do my other clean up work?

Thanks

Upvotes: 2

Views: 3688

Answers (3)

neverwinter
neverwinter

Reputation: 810

You can use a thread for monitoring ThreadPoolExecutor like that

import java.util.concurrent.ThreadPoolExecutor;

public class MyMonitorThread implements Runnable {
     private ThreadPoolExecutor executor;

        private int seconds;

        private boolean run=true;

        public MyMonitorThread(ThreadPoolExecutor executor, int delay)
        {
            this.executor = executor;
            this.seconds=delay;
        }

        public void shutdown(){
            this.run=false;
        }

        @Override
        public void run()
        {
            while(run){
                    System.out.println(
                        String.format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s",
                            this.executor.getPoolSize(),
                            this.executor.getCorePoolSize(),
                            this.executor.getActiveCount(),
                            this.executor.getCompletedTaskCount(),
                            this.executor.getTaskCount(),
                            this.executor.isShutdown(),
                            this.executor.isTerminated()));
                    try {
                        Thread.sleep(seconds*1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }

        }

}

And add

MyMonitorThread monitor = new MyMonitorThread(executorPool, 3);
            Thread monitorThread = new Thread(monitor);
            monitorThread.start(); 

to your class where ThreadPoolExecutor is located.

It will show your threadpoolexecutors states in every 3 seconds.

Upvotes: 1

Evgheni Crujcov
Evgheni Crujcov

Reputation: 470

If you Worker class implements Callable instead of Runnable, then you'd be able to see when your threads complete by using a Future object to see if the Thread has returned some result (e.g. boolean which would tell you whether it has finished execution or not).

Take a look in section "8. Futures and Callables" @ website below, it has exactly what you need imo: http://www.vogella.com/articles/JavaConcurrency/article.html

Edit: So after all of the Futures indicate that their respective Callable's execution is complete, its safe to assume your executor has finished execution and can be shutdown/terminated manually.

Upvotes: 4

babanin
babanin

Reputation: 3584

Something like this:

    exec.shutdown();

    // waiting for executors to finish their jobs
    while (!exec.awaitTermination(50, TimeUnit.MILLISECONDS));

    // perform clean up work

Upvotes: 3

Related Questions