I have a very basic question regarding the for loop. When we call a method (return or void) on some element inside the for each loop, does the loop waits until the method finishes or does it move on to the next element in the collection immediately?
2 Answers
Unless the method is run in a new thread, the next iteration of the loop will not start until the previous one is completed
That you are asking this question indicates to me that you may not fully understand the concept of the Thread stack (vs the global Heap). I'm not trying to be mean but understanding how the thread stack works is very important
Comments
It will run through the method before moving on to the next element.
3 Comments
Kenny Freeman
regardless it's going to finish before moving on. Here is a site that gives you the basics on how for loops work. beginnersbook.com/2015/03/for-loop-in-java-with-example
Kenny Freeman
contrary to belief computers can only do one thing at a time. Here is another site that explains how they 'multitask'. tldp.org/HOWTO/Unix-and-Internet-Fundamentals-HOWTO/…
ajb
@KennyFreeman Even if there are multiple processors? By the way, has the page that you pointed to been updated in the last almost 20 years? Doesn't look like it.