filmov
tv
Java concurrency example -- Load the ark with multithreading part 1

Показать описание
Local variables are thread safe, because they are confined in the stack of the calling method. Local variable itself don't have to be threadSafe, even though multiple threads are calling the same method, the local variables are not shared among them. Local variables only exists in the method's stack, thus confined in the calling thread's stack.
This version have several problem, the helper thread should start earlier by the main thread in order to be helpful, once you do that, the hasAminal can throw ConcurrentModificationException. Also, did the author intentionally not override the equals method in AnimalPair class so that the Ark (also not a singleton) can load duplicate pairs? I will reveal these bugs by stepping through threads execution and show the difference of local variable and instance variable with eclipse debugger in the next section.