1

I have the below program which send the mail using java mail api , now this the is the simple program i have developed now i want to modify in terms of parallel execution by using executorframework 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

lets say there are five different threads t1,t2,t3,t4 and t5 then all of them should independently hit my function which is main(@) is calling rite now but at the same time

below is my java code

public class SSendEmail {

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

        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

2 Answers2

0

I think you would use a ScheduledExecutorService and call it like this.

ScheduledExecutorService exec = Executors.newScheduledThreadPool(amount);
for (int i = 0; i < amount; i++) {
    exec.schedule(yourMailSendingRunnable, delay, TimeUnit.MILLISECONDS);
}

You should replace amount, yourMailSendingRunnable and delay to account for your needs.

Cooki3Tube
  • 107
  • 1
  • 10
  • @Cooki3Tube..Thanks for the quick response , request you to please if you can convert my piece of code into it ans show me i want 5 different threads and all threads simultaneously make a call to the method xxsendmail . – sss Apr 08 '16 at 16:17
0

As long your only requirement is that 5 threads should work concurrent, you are done with something like this:

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

You will use an ExecutorService when more control is needed. E.g. ThreadPooleExecutor to limit the number of concurrent running threads when you have continues new threads, but you want limit to, say, 10 threads running at the same time.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • Thanks agree with you , but i also want that five independent thread should call my method named xxsendmail at the same time itself , please advise that above code will make sure that 5 independent threads are calling my method named xxsendmail simentously – sss Apr 08 '16 at 16:24
  • @sss: Try Peter's code and see for yourself. I hope your email provider doesn't block you as an email spammer. – Gilbert Le Blanc Apr 08 '16 at 16:58
  • @Gilbert well we have test mailbox set up so don't have to worry for that my only concern is that all threads should be independent plus they should call the method at same time that's all please – sss Apr 08 '16 at 17:05
  • `please advise that above code will make sure ..`, in multi-threading very few things are for **sure** (or deterministic). When you do `Thread#start()` the JVM will do the *best* to get the job done. http://stackoverflow.com/questions/3830347/can-a-multi-threaded-program-ever-be-deterministic – PeterMmm Apr 09 '16 at 08:30