I have a cronjob,
我有一个cronjob,
users
id company_id
1 1
2 1
...
10000 1
cron_events
id event_name company_id
1 birthday_party 1
So here i need to send email to all the users of the company 1,
所以我需要发送电子邮件给公司的所有用户1,
i know how to do this by one by,
我知道如何做到这一点,
but i wanted to do this by multithreading concept, i am new to thread concept, anyone can guide me on this,
但我想通过多线程概念来做到这一点,我是线程概念的新手,任何人都可以指导我这个,
this is my existing code,
这是我现有的代码,
this is my Controller class
这是我的Controller类
@Scheduled(fixedDelay = 15000)
public void sendNotificationToUser() {
Future<Boolean> sendNotificationToUser = cronService.sendNotificationToUser();
if (sendNotificationToUser.isDone()) {
logger.info(
"-->Notification [Sent] to Patient ");
}
}
this is my implementation class
这是我的实现类
@Async
@Transactional
public Future<Boolean> sendNotificationToUser() {
logger.info("-->in the function of sendNotificationToUser Function");
List<User> users = cronReferralRepository.findByCompanyIdAndActive(1L, true);
for (User user : users) {
Future<Boolean> futureCampaignCreated = processUser(user);
}
return new AsyncResult<Boolean>(true);
}
@Async
private Future<Boolean> processUser(User user) {
System.out.println("Thread Name --> " + Thread.currentThread().getName());
Long userId = user.getId();
String patientFirstName = null;
String patientLastName = null;
if (user != null) {
patientFirstName = user.getFirstName();
patientLastName = user.getLastName();
}
String patientFullName = patientFirstName + " " + patientLastName;
//send mail code
...
}
Still my thread printing same thread name, how its possible, but i am expecting a different thread name,
仍然我的线程打印相同的线程名称,它是如何可能的,但我期待一个不同的线程名称,
1 个解决方案
#1
0
Multithreading essentially means running code on parallel threads. Instead of having a sequential code execution you split this execution in multiple branches.
多线程本质上意味着在并行线程上运行代码。您无需执行顺序代码,而是将此执行拆分为多个分支。
Main thread Main thread | | | | | |__ __ __ | send mail M1 | | | | | send mail M2 --> | M1 | M3 | send mail M2 * M2 | send mail M3 | | *
主线程主线程| | | | | | __ __ __ |发送邮件M1 | | | | |发送邮件M2 - > | M1 | M3 |发送邮件M2 * M2 |发送邮件M3 | | *
By default your code exploit a single thread, but when an @Asynch method is called, the execution of this method is handled in a new thread.
默认情况下,您的代码利用单个线程,但是当调用@Asynch方法时,将在新线程中处理此方法的执行。
What I think you want to achieve is to send mail in parallel threads, one for each user. But, in your code, in order to exploit multiple threads you have to move the for loop before calling the @asynch method, so that every calls to this method will create a new thread.
我认为你想要实现的是以并行线程发送邮件,每个用户一个。但是,在您的代码中,为了利用多个线程,您必须在调用@asynch方法之前移动for循环,以便每次调用此方法都将创建一个新线程。
Every thread created by the @Asynch is "destroyed" as soon as the @asynch methods completes.
@asynch方法完成后,@ Asynch创建的每个线程都会被“销毁”。
Now the main thread can wait the end of all the called @asynch, or not based on your requirements. Do you need to wait that all mails have been sent? Or do you want to proceed in your main thread execution immediately after calling the @Asynch method?
现在主线程可以等待所有被调用的@asynch的结束,或者不是根据您的要求。你需要等待所有邮件都被发送了吗?或者,您是否希望在调用@Asynch方法后立即执行主线程执行?
Case 1 waiting that all mails are sent.
案例1等待所有邮件发送。
Main thread | | |__ __ __ ° | | | ° M1 | M3 | M2 | | | *
主线程| | | __ __ __°| | | °M1 | M3 | M2 | | | *
Case 2 procede execution without waiting thread results.
案例2执行执行而不等待线程结果。
Main thread | | |__ __ __ | | | | | M1 | M3 | M2 | *
主线程| | | __ __ __ | | | | | M1 | M3 | M2 | *
After you have answered those questions take further readings on Future and thread joins.
在回答完这些问题之后,请进一步阅读Future和thread join。
Remember that with spring boot you can easily configure the number of parallel threads, the queue size and other useful settings.
请记住,使用spring boot,您可以轻松配置并行线程数,队列大小和其他有用设置。
#1
0
Multithreading essentially means running code on parallel threads. Instead of having a sequential code execution you split this execution in multiple branches.
多线程本质上意味着在并行线程上运行代码。您无需执行顺序代码,而是将此执行拆分为多个分支。
Main thread Main thread | | | | | |__ __ __ | send mail M1 | | | | | send mail M2 --> | M1 | M3 | send mail M2 * M2 | send mail M3 | | *
主线程主线程| | | | | | __ __ __ |发送邮件M1 | | | | |发送邮件M2 - > | M1 | M3 |发送邮件M2 * M2 |发送邮件M3 | | *
By default your code exploit a single thread, but when an @Asynch method is called, the execution of this method is handled in a new thread.
默认情况下,您的代码利用单个线程,但是当调用@Asynch方法时,将在新线程中处理此方法的执行。
What I think you want to achieve is to send mail in parallel threads, one for each user. But, in your code, in order to exploit multiple threads you have to move the for loop before calling the @asynch method, so that every calls to this method will create a new thread.
我认为你想要实现的是以并行线程发送邮件,每个用户一个。但是,在您的代码中,为了利用多个线程,您必须在调用@asynch方法之前移动for循环,以便每次调用此方法都将创建一个新线程。
Every thread created by the @Asynch is "destroyed" as soon as the @asynch methods completes.
@asynch方法完成后,@ Asynch创建的每个线程都会被“销毁”。
Now the main thread can wait the end of all the called @asynch, or not based on your requirements. Do you need to wait that all mails have been sent? Or do you want to proceed in your main thread execution immediately after calling the @Asynch method?
现在主线程可以等待所有被调用的@asynch的结束,或者不是根据您的要求。你需要等待所有邮件都被发送了吗?或者,您是否希望在调用@Asynch方法后立即执行主线程执行?
Case 1 waiting that all mails are sent.
案例1等待所有邮件发送。
Main thread | | |__ __ __ ° | | | ° M1 | M3 | M2 | | | *
主线程| | | __ __ __°| | | °M1 | M3 | M2 | | | *
Case 2 procede execution without waiting thread results.
案例2执行执行而不等待线程结果。
Main thread | | |__ __ __ | | | | | M1 | M3 | M2 | *
主线程| | | __ __ __ | | | | | M1 | M3 | M2 | *
After you have answered those questions take further readings on Future and thread joins.
在回答完这些问题之后,请进一步阅读Future和thread join。
Remember that with spring boot you can easily configure the number of parallel threads, the queue size and other useful settings.
请记住,使用spring boot,您可以轻松配置并行线程数,队列大小和其他有用设置。