So we say that I have a fixed variable, which is an array of size 5. And let's say I have two threads, T1 and T2, both of them are trying to change the element in index 0 of that array. And then use the element on element 0. In this case, I should lock the element until T1 is eliminated by using that element?
Another question is that T1 and T2 are already running, T1 access element index first, then lock it but then after T2 the index tries to use the element on 0 , But T1 has not unlocked Index 0 yet, in this case, to use the element 0 for the T2, what should T2 do? Should T1 use back in the call function, when T1 unlocked the index 0 of the array?
Synchronization in Java (technically) about refusing to access other threads Not at all, it is about ensuring the unique use of the thread between the thread (at one time) using the lock. Therefore T2 can access the object, while T1 synchronization is locked, but T1 will be unable to get synchronization lock until release.
Comments
Post a Comment