java - How do we know threadPoolExecutor has finished execution -


i have parent thread sends messages mq , manages threadpoolexecutor worker threads listen mq , writes message output file. manage threadpool of size 5. when run program, have 5 files messages. works fine until here. need merge these 5 files in parent thread.

how know threadpoolexecutor finished processing 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();         }         (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();         (int = 0; < 5; i++) {             worker worker = new worker(i, this.messagereciever, this.jmsreplyqueue);             exec.execute(worker);         }         exec.shutdown();          if(exec.isterminated()){ //problem here. control never gets here.             long currenttime = new date().gettime() - finishdate;             system.out.println("time taken: "+currenttime);             mergefiles();         }     } } 

this 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 file");             stop = true;         }     } } 

how know when threadpool has finished processing can other clean work?

thanks

something this:

    exec.shutdown();      // waiting executors finish jobs     while (!exec.awaittermination(50, timeunit.milliseconds));      // perform clean work 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -