java - Can this code have race condition? -
public static void deletelast(vector list) { int lastindex = list.size() - 1;//line 2 list.remove(lastindex); // line 3 } i known vector threadsafe in java can case happen
let's in case has list.size()=10
thread calls deletelast , @ line 2 lastindex = 9 .it stops reason
thread b call deletelast , @ line 2 lastindex = 9.it goes line 3 , list has 9 elements
thread wakes , goes line 3 tries remove object @ index 9 doesn't exist , have exception here
sure. you've correctly identified race condition.
Comments
Post a Comment