Inconsistent out of sync

I got this violation in the return statement in the following method:

protected Token getAccessToken() {  
    synchronized (this) {
        if (token == null || isExpired(token))
            token = createToken();
    }

    return token; // <-- Inconsistent synchronization of blablabla.token; locked 75% of time
}

Are there any visibility issues related to the field token? As I understand it, after synchronizedthe block marker should have its last value.

Am I missing something or falsely positive?

+4
source share
2 answers

Consider the following:

  • Thread1: introduces a method
  • Thread2: introduces a method
  • Thread1: introduces a synchronization block, the token is not null and has not expired.
  • Thread1: Exit Sync Block
  • Thread2: enters the synchronization block, the token is not zero, but expired.
  • Thread2: Assigns a New Token
  • Thread1: ( , 2, )
  • Thread2:
  • Thread2: ()

, , token ( !), token .

, . ( ) token (, null), , , token ( ), null .

+4

Thread A , B, .

, B , B . , . .

+3

All Articles