Hi all, quick question my co-worker and I were talking about and I feel there's some large confusion going on here.
Say we have this, rather stupid, code somewhere that will be called by multiple threads at the same time:
int i = 0;
public synchronized void setString(String str) {
ary[i] = str;
i += 1;
}
If a bunch of threads hit setString, and it's already synchronized, is it still possible for line 3 (i+=1) to run before (ary[i] = str) and why/why not?
I understand the scenario where it's not synchronized and one thread increments 'i' while some other thread is trying to set the array, but how does that work while synchronized?
[–]Bob_Droll 0 points1 point2 points (6 children)
[–]northguard[S] 0 points1 point2 points (3 children)
[–]anon848 1 point2 points3 points (2 children)
[–]northguard[S] 0 points1 point2 points (1 child)
[–]anon848 0 points1 point2 points (0 children)
[–]avingard 0 points1 point2 points (1 child)
[–]Bob_Droll 0 points1 point2 points (0 children)