Threading: Locking an object?
There is something I just can't wrap my head around...
I have a class with a private String "myString".
That class has a getMyString() which returns the string and a setMyString() which sets the string.
If I have one thread accessing getMyString() and another accessing setMyString(), things will go wrong, right?
Then, how would I acquire a lock on myString?
Everyone says to "just" use a synchronized block, but I just can't understand how to do such thing in this case!
if I make both getMyString() and setMyString() methods synchronized, that would only prevent two threads from accessing the SAME method, right?
Please, help me understand because I'm having issues understanding how Java handles this!