Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

using executorService

HI,
In my spring web application there is a class like : 

[code]
@Service
class BotMessage {
    private ExecutorService executorService = Executors.newFixedThreadPool(1);
     public void sendMessage(message) {
     executorService.submit(() -> sendMessage(message));
}
}
[/code]

In another class : 
[code]
@Service
Public class PullService {
    private ExecutorService executorService = Executors.newFixedThreadPool(10);
   public void pull(Date date) {
        List<UserInfo> getAllUsers = userTokenService.getAllUsers();
        getAllUsers.forEach((o) -> {
                        pullGlobalNotification(o, date, accessToken);
                  }
}
}


 private void pullGlobalNotification(UserInfo userInfo, Date date, String accessToken) {
            GitHub gitHub = githubCache.getIfPresent(userInfo.getUserId());
            executorService.submit(notificationTask);
}[/code]

Now this notificationTask has a run function() {
for(int i = 0;i<10;i++)
botMessage.sendMessage(i.toString());
}

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.getAllUsers();
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
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rohit Bajaj

ASKER

thanks
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 ?
Avatar of dpearson
dpearson

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