gpt4 book ai didi

java - Lockstep在编程中是什么意思?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:08:34 24 4
gpt4 key购买 nike

我一直在阅读 Effective Java

Item 46: Prefer for-each loops to traditional for loops

在提到需要迭代器/for循环而不是for-each循环的情况的部分,有一点:

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.

现在,我明白了对迭代器/索引变量的显式控制意味着什么(不是每个循环的 Controller )。但是我无法理解这个意义上的lockstep的含义。我试着用谷歌搜索它并找到了一篇关于 Wikipedia 的文章其中指出:

Lockstep systems are fault-tolerant computer systems that run the same set of operations at the same time in parallel.

这我理解为具有例如用于故障转移的服务器的附加实例没关系。但是我无法完全理解在编程中迭代集合的上下文中的确切含义。

最佳答案

在这个语境下,意思更像是military marching .

或者,当一个操作前进时,其他操作也随之前进/跟随。

或者更具体地说,如果你想遍历两个集合,你不能轻易地使用 foreach 结构:

for (Item i : list1) { //only allows you to iterate over 1 list.

}

遍历 2 个集合 )

Iterator iter1 = list1.iterator();
Iterator iter2 = list2.iterator();
while (iter1.hasNext() && iter2.hasNext()){
Item a = iter1.next();
Item b = iter2.next();
doSomething(a, b);
}

即在迭代 list1 时,迭代 list2 随之而来——“步调一致”

关于java - Lockstep在编程中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22260322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com