I have a method sendMail(list)
. This method will send the mails to the recipients which are there in the list.
public void sendMail(List<DTO> dto) {
for(DTO individualObject: dto) {
bulkMailSender.sendSimpleMessage(individualObject.getEmail(),masterDetails.getMailSubject() , content, masterDetails.getMailFrom(), individualObject);
try {
TimeUnit.MINUTES.sleep(Long.parseLong(individualObject.getTimegap().trim()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
I have this kind of method. I want to run this method Thread based, when one thread is executing the mails, I should allow the other thread to access sendMail
and send simultaneously together. Each and every individualObject
contains it's own sleep time.
How can I make it worked with the multiple threads.
Let's take an example
import java.util.concurrent.TimeUnit;
public class SleepClass {
public static void main(String[] args) {
SleepClass s= new SleepClass();
s.m1(10000);
s.m1(20000);
}
public void m1(int time) {
for(int i = 0; i< 3; i++) {
System.out.println(i);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
In the above example I have a regular method and it is executing one by one. How can make it simultaneous execution