I have been reading Effective Java
on
Item 46: Prefer for-each loops to traditional for loops
In the part where are mentioned the cases when is iterator/for loop needed isntead of for-each loop, there is this point:
Parallel iteration—If you need to traverse multiple collections in parallel, then you need explicit control over the iterator or index variable, so that all iterators or index variables can be advanced in lockstep.
Now, I understand what explicit control over iterator/index variable mean (not controller by for each loop). But I could not understand the meaning of lockstep in this sense. I tried to google it and found an article on Wikipedia which states:
Lockstep systems are fault-tolerant computer systems that run the same set of operations at the same time in parallel.
This I understand as having aditional instance of for example server for fail-over That's ok. But I fail to fully understand what could be the exact meaning in the context of iterating over collection in programming.
Definition of lockstep 1 : a mode of marching in step by a body of persons going one after another as closely as possible. 2 : a standard method or procedure that is mindlessly adhered to or that minimizes individuality.
Lockstep synchronization involves two or more similar devices sharing the same timing and triggering and essentially acting as a single device. Sharing a sample clock between analog input and analog output operations on a single device is also considered lockstep synchronization.
Lockstep mode provides protection against multi-bit memory errors that occur on the same DRAM device. Lockstep mode can correct any single DRAM device failure on x4 and x8 DIMM types. The DIMMs in each channel must have identical HP part numbers.
History. Originally it was used in drilling soldiers. Each soldier stepped on the point just vacated by the foot of the soldier in front of him. Thus the soldiers stayed in position to form close files. Lockstep marching was a characteristic trait of American prisons of the 19th century.
In this context, the meaning is more like the military marching.
Or, when one operation advances, other operations advances/follows with it.
Or more specifically, if you want to iterate over two collections, you cannot easily the foreach construct:
for (Item i : list1) { //only allows you to iterate over 1 list.
}
Iterate over 2 collections )
Iterator iter1 = list1.iterator();
Iterator iter2 = list2.iterator();
while (iter1.hasNext() && iter2.hasNext()){
Item a = iter1.next();
Item b = iter2.next();
doSomething(a, b);
}
i.e. while iterating list1, iterating list2 follows with it - "in lockstep"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With