java call unsynched from synched

Viewed 31
public synchornized void mySyncedMethod(int myVar){
  unsynchedMethod(int myvar);
}

private void unsynchedMethod(int myVar){
  ...
  do lots of processing
  ...
  incrementStateVar += myVar;
}

thread A starts, then thread B starts. If thread B processing takes less time than A, could B complete (and increment state var) before A.

1 Answers

If two threads call a synchronized method of the same object, only one can enter, the other waits until the first one exits and unlocks the object. So if threads A and B call the synchronized method of the same object, only one can increment the var.

Related