0

我的代码:


我用相同的数字运行了 3 次,输出:

错误的!

the FibonacciNumber test Started!!!
the FibonacciNumber(8) is 
 20

这是对的

the FibonacciNumber test Started!!!
the FibonacciNumber(8) is 
 21

错误的!

the FibonacciNumber test Started!!!
the FibonacciNumber(8) is 
 19
4

1 回答 1

0

拥有一个由多个参与者同时更新的全局变量是不安全的。多个参与者可以同时处理消息,如果两个或更多参与者尝试同时更新这些变量,则不会给出正确的结果。因此,使用此算法,您至少需要 makecounterresultinto AtomicInteger

改进算法以避免全局变量要好得多。状态信息应该使用请求和回复在参与者之间传递,而不是在多个参与者同时访问的变量中

于 2020-03-23T08:06:23.527 回答