We help IT Professionals succeed at work.

using executorService

Rohit Bajaj
Rohit Bajaj asked
on
253 Views
Last Modified: 2017-05-22
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
Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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 ?
CERTIFIED EXPERT

Commented:
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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.