Rohit Bajaj
asked on
using executorService
HI,
In my spring web application there is a class like :
[code]
@Service
class BotMessage {
private ExecutorService executorService = Executors.newFixedThreadPo ol(1);
public void sendMessage(message) {
executorService.submit(() -> sendMessage(message));
}
}
[/code]
In another class :
[code]
@Service
Public class PullService {
private ExecutorService executorService = Executors.newFixedThreadPo ol(10);
public void pull(Date date) {
List<UserInfo> getAllUsers = userTokenService.getAllUse rs();
getAllUsers.forEach((o) -> {
pullGlobalNotification(o, date, accessToken);
}
}
}
private void pullGlobalNotification(Use rInfo userInfo, Date date, String accessToken) {
GitHub gitHub = githubCache.getIfPresent(u serInfo.ge tUserId()) ;
executorService.submit(not ificationT ask);
}[/code]
Now this notificationTask has a run function() {
for(int i = 0;i<10;i++)
botMessage.sendMessage(i.t oString()) ;
}
What i am wondering is since botMessage.sendMessage uses an executor with size 1.
now at i = 0 sendMessage will internally call [b]executorService.submit( () -> sendMessage(message));
[/b]
At i = 1... Will the call to [b]executorService.submit( () -> sendMessage(message));[/b] block if the previous call is still running as the size of executor is 1. Also will it get queued in the executor and the loop will still continue ? What should be the ideal size in this case.. As this function may be called simultaneously from various thread..
Also in the PullService class the size of ThreadPool is 10... Althought the number of users returned by userTokenService.getAllUse rs();
may be more than 10.. In that case the further calls will block ? Also what should be the ideal size of ThreadPool to keep here.
Thanks
In my spring web application there is a class like :
[code]
@Service
class BotMessage {
private ExecutorService executorService = Executors.newFixedThreadPo
public void sendMessage(message) {
executorService.submit(() -> sendMessage(message));
}
}
[/code]
In another class :
[code]
@Service
Public class PullService {
private ExecutorService executorService = Executors.newFixedThreadPo
public void pull(Date date) {
List<UserInfo> getAllUsers = userTokenService.getAllUse
getAllUsers.forEach((o) -> {
pullGlobalNotification(o, date, accessToken);
}
}
}
private void pullGlobalNotification(Use
GitHub gitHub = githubCache.getIfPresent(u
executorService.submit(not
}[/code]
Now this notificationTask has a run function() {
for(int i = 0;i<10;i++)
botMessage.sendMessage(i.t
}
What i am wondering is since botMessage.sendMessage uses an executor with size 1.
now at i = 0 sendMessage will internally call [b]executorService.submit(
[/b]
At i = 1... Will the call to [b]executorService.submit(
Also in the PullService class the size of ThreadPool is 10... Althought the number of users returned by userTokenService.getAllUse
may be more than 10.. In that case the further calls will block ? Also what should be the ideal size of ThreadPool to keep here.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes that's right, you could end with lots of context switches.
But usually the size of the thread pool isn't that critical. You can just pick a reasonable number and it should be fine. It'll be rare cases where the size of the pool is more important than the efficiency of the task implementation.
Doug
But usually the size of the thread pool isn't that critical. You can just pick a reasonable number and it should be fine. It'll be rare cases where the size of the pool is more important than the efficiency of the task implementation.
Doug
ASKER
also from what you have explainer it looks like that if i keep the thread pool too large it may be the case that lot of context switching might happen between threads ?