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[] = {"[email protected]" }; String mailCc[] = {"[email protected]" }; xxsendmail(mailTo, mailCc, "sendername", "testsubject.", "testsubject..", smtpHost , mailSmtpPort); } 

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@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 .
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[] = {"[email protected]" }; String mailCc[] = {"[email protected]" }; 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.

4 Comments

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: Try Peter's code and see for yourself. I hope your email provider doesn't block you as an email spammer.
@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
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. stackoverflow.com/questions/3830347/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.