英文:
how to call a method with multiple threads
问题
我有一个名为sendMail(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();
}
}
}
我有这样一种方法。我希望基于线程运行此方法,当一个线程正在执行邮件时,我应该允许其他线程访问sendMail并一起同时发送。每个individualObject都包含其自己的睡眠时间。
如何使用多个线程使其工作?
让我们来看一个示例:
import java.util.concurrent.TimeUnit;
public class SleepClass {
public static void main(String[] args) {
SleepClass s = new SleepClass();
new Thread(() -> s.m1(10000)).start();
new Thread(() -> s.m1(20000)).start();
}
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();
}
}
}
}
在上面的示例中,我有一个常规方法,它一个接一个地执行。如何使其同时执行,请参阅此链接。
英文:
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
答案1
得分: 1
你需要将你的逻辑放入一个Runnable中,并使用new Thread(runnable).start()来启动它。要将参数传递给每个runnable,请将它们定义为类变量,以便您可以通过构造函数传递它们,并在run方法中使用它们:
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++) {
new Thread(new Launcher(i,time)).start();
}
}
public class Launcher implements Runnable {
int i;
int time;
public Launcher(int i, int time) {
this.i=i;
this.time=time;
}
@Override
public void run() {
System.out.println(i);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
英文:
You have to put your logic in a Runnable and launch it using new Thread(runnable).start().
To pass parameters to each runnable define them as class variables so you can pass them via the constructor and use them in the run method:
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++) {
new Thread(new Launcher(i,time)).start();
}
}
public class Launcher implements Runnable {
int i;
int time;
public Launcher(int i, int time) {
this.i=i;
this.time=time;
}
@Override
public void run() {
System.out.println(i);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
答案2
得分: 1
如果您需要同时执行并且每次都需要一个新线程,您可以在这里找到解决方案:
public class SleepClass {
public static void main(String[] args) {
SleepClass s = new SleepClass();
s.m2(500);
s.m2(1000);
}
public void m2(int time) {
SleepClass s = new SleepClass();
new Thread(() -> {
s.m1(time);
}).start();
}
public void m1(int time) {
for (int i = 0; i <= 10; i++) {
System.out.println(i);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
英文:
if you need simultaneous execution and each time new thread you can find the solution here
public class SleepClass {
public static void main(String[] args) {
SleepClass s= new SleepClass();
s.m2(500);
s.m2(1000);
}
public void m2(int time) {
SleepClass s= new SleepClass();
new Thread(() -> {
s.m1(time);
}).start();
}
public void m1(int time) {
for(int i = 0; i<= 10; i++) {
System.out.println(i);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论