0

I have the below program which send the mail using java mail api ,i want to modify in terms of parallel execution by using executor framework that i want that 5 different threads independently should trigger my this program but those 5 different threads should trigger simultaneously at the same time , please advise how to modify the below piece of code so that i can add executor framework in it and that will divide the task of sending the mails into five different threads and those independent 5 threads will send the mail at the same time

public class SSendEmail implements Runnable {

   public static void main(String [] args) throws Exception, IOException, Exception{

      for(int i=0;i<5;i++) {
          new Thread(new SSendMail()).start();
      }
   }

   public void run() {

    String smtpHost = "xxx";
    String mailSmtpPort = "000";
    String mailTo[] = {"sart@wer.com" };
    String mailCc[] = {"sart@wer.com" };

    xxsendmail(mailTo, mailCc, "sendername",
            "testsubject.", "testsubject..", smtpHost , mailSmtpPort);
  }  
}
sss
  • 161
  • 1
  • 1
  • 13
  • Can you include why you believe this code is not doing what you want? – Peter Lawrey Apr 09 '16 at 09:16
  • I think he should be asking for more information from the person who gave him this code the last time he asked this. – Kayaman Apr 09 '16 at 09:20
  • Well folks sorry but this time can you please advise my how to achieve the same thing through executor framework – sss Apr 09 '16 at 09:21
  • @kayaman sorry to interrupt but please understand I am new to the world of multithreading in java – sss Apr 09 '16 at 09:22
  • @peter completely agree with you my I tension is to check that in for loop first thread will be created and then second one an so on all the threads will not be created at same time so a little time difference is there – sss Apr 09 '16 at 09:24
  • @sss threads are designed to run independently, so there will always be a little difference. An ExecutorService wraps Threads as a convenience but they are still threads all the same. Even if you call the method `xxsendmail` at the same clock cycle, you can assume there packets will be received by the server serially unless each is using a different network adapter. – Peter Lawrey Apr 09 '16 at 09:27
  • @sss if you have one network adapter or the other end has one network adapter it can only be sending/receiving one packet at a time, in a potentially random order you have no control over when send within a short time frame. – Peter Lawrey Apr 09 '16 at 09:30
  • OK Thanks request you to show a situation where 5 independent threads try to hit mailbox at same time since I know if 5 independent thread will hit the mailbox at same time then socket connection reset error will come since I have configure mailbox in such a way so that one one thread can hit it at a time – sss Apr 09 '16 at 09:30
  • I just want to produce socket connection reset error by java mail that's why I am trying this – sss Apr 09 '16 at 09:31

0 Answers0