https://codegym.cc/quests/lectures/questcore.level07.lecture01
The "another example" in the lesson, with the swaping of the names.
As Dmitri said in his post at 29 December 2020, 06:07:
"if each thread is working with it's own MyClass instance why all this mess should happen?"
In my opinion, this presented possibility in the example can only occur in the case of static variables.
Is this possible?? Isn't it just another misspelling again?
Resolved
Comments (10)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
26 April 2021, 19:56
The example is two threads working on the same object, not each thread working on separate objects.
0
Gellert Varga
26 April 2021, 20:05
Is this that case when we create more and more new Thread objects with the same Runnable object?
0
Guadalupe Gagnon
26 April 2021, 20:11
No, it all depends on the code in question. You can set it up that way, or any other way you can think of... and any way that you are not able to think of as yet.
0
Gellert Varga
26 April 2021, 20:24
Do you find the example code in the lesson precise enough to help us understand this topic?...
Is it possible maybe something is missing from the code or from the explanation?... Just for better understanding.
0
Gellert Varga
26 April 2021, 21:17
And what do String s1 and String s2 mean in the example? Why are there two different s variables in the explanation? If two threads work on a single one MyClass object, then this object has only one piece of s-variable, isn't right?
0
Guadalupe Gagnon
27 April 2021, 03:11
Its color coded to show exactly what is happening concurrently, green is one thread and blue is the other. The swap() method has its own local variable 's' which is demonstrated in that code as 's1' and 's2' because it isn't shared between the threads. They could have used 's(thread1)' and 's(thread2)', though I don't know if that is better or not. The class state (the variables name1 and name2) however can be corrupted when not coded as thread safe. .
In the example it shows both threads calling the swap method on the 1 object of MyClass. This can very easily cause problems as the code demonstrates: when the first thread (in green) changes the value of name1 of the object but the CPU swaps to the next thread (the blue lines) before the code reaches the third line the blue method captures the wrong value. This leads to one name being lost so both name1 and name2 would be Lena instead of Lena and Ally.
It is unlikely that this will happen if you just code this up and run it as the method processes so fast it is unlikely to be interrupted, however if you loop it to run a million times in a row it most likely will occur. That is the thing, the examples are showing you what can, and does, occur in the real word with much larger programs, but they can't put a real world demonstration of 10000 lines of code in these small lessons.
0
Gellert Varga
27 April 2021, 12:53
Thanks for the explanation!
Please verify me if I understand correctly:
So both threads modify the same class state (name variables), but both threads have their own variable "s"?
Do each thread make a replica (its own instance) of the local variables of the methods?
(This explanation was missing from the lesson, and I can’t figure out for myself how these things work.)
0
Guadalupe Gagnon
27 April 2021, 13:14
Every method call gets its own "instance", and any variables declared within are not shared, including the method parameters. If a method does not change Object state (or any other shared resource) then it is implicitly thread safe.
In the opposite, where Object state is modified (or a shared resource) from a method, then that method would not be considered thread safe without having the proper code for it.
0
Gellert Varga
27 April 2021, 16:35
I understand. Now much clearer!
And what does "any other shared resource" mean? Static variables, for example?
0
Guadalupe Gagnon
27 April 2021, 17:29
yes, static variables are one example. Hard drive data, data bases, network resources, etc... are all examples as well.
0