Hey Seema,
First of all thanks for taking the time to read the article.
I am glad the class genius went through this. :)
And thanks for pointing this out. `oAuthToken` could very well be marked volatile for this specific example.
Lets consider some scenarios:
1. What if the `refreshAuthToken()` fails? I mean, the POST request times out due to network issue? Or say the API server is down? Maybe we would want to retry the API call N times (with some delay between each retry)? Lets say we run a loop N times to refresh the token upon failure, and break out of the loop upon success. Will it ensure that no thread reads `oAuthToken` while I am still retrying to refresh the token? To be specific, as the loop runs for the 1st time, and the refresh fails, and before it starts running for the 2nd time, are there not any chances for any reading thread to read the old `oAuthToken` and use it which would result in `callApi()` to fail? We would need some way to ensure the whole retry loop is atomic. Right?
2. Even in this specific example, the refresh happens every 45 minutes. In these 45 minutes, we will have more than a thousand read requests. Marking the variable `volatile` would make it uncacheable. Do you think we should be reading the `oAuthToken` variable from the main memory and not from local thread cache where our number number of read operations >>> number of write operations?
What are your opinions about these?