my code:
I ran it three times with the same number, the output:
Wrong!
the FibonacciNumber test Started!!!
the FibonacciNumber(8) is
20
This is correct
the FibonacciNumber test Started!!!
the FibonacciNumber(8) is
21
Wrong!
the FibonacciNumber test Started!!!
the FibonacciNumber(8) is
19
It is not safe to have a global variable that is updated by multiple actors at the same time. Multiple actors can be processing messages at the same time, and if two or more actors try to update those variables at the same time, it will not give the correct result. So with this algorithm you at very least need to make counter
and result
into AtomicInteger
.
It is much better to improve the algorithm to avoid global variables. The state information should be passed between the actors using requests and replies, rather than being in variables that are accessed by multiple actors simultaneously