Reputation: 1087
If I have a shared object, that is held in a httpsession.
If I modify it time to time without any concurrent changes (for example my customer information is held in session and is accessible ONLY for one and the same user at a time(owner of this info), is there a chance to have some kind of problems, related with multi-threading and so on?
Example of flow: object is kept in session(thread 1). object is modified by thread, that is serving request1(user changed his data and save it in session). object is modified by thread, that is serving request2 (user opens page with his user information).
Is there a chance, that data on page, that is rendered by request2 will be stale(no ajax or other asynchronous stuff is done)?
UPD: My problem is, that it feels like it is sequential access to shared resource, that is held in httpsession(bound to concrete logged in user). That's the main reason, thy I don't want to use volatile stuff or synchronization.
Upvotes: 1
Views: 300
Reputation: 200168
You haven't given enough relevant information in terms of threads to answer your question precisely, but let me give you some general warnings.
In order to experience problems stemming from data races, there is no need for truly concurrent access—access from more than one thread is enough, even if at moments separated by seconds or minutes.
The problems you can experience stem from visibility of changes by one thread to other threads. In the absence of happens-before
relationships between reads and writes the JVM is not required to propagate any changes from local caches/thread-local storage to main memory, and vice-versa: the reading thread is not required to pull the fresh data from the main memory.
Depending on the exact code you have, sometimes the just-in-time compiler can even optimize away the entire read operation, noticing that it is enough to read the first time and cache that value forever (since there is no happens-before
relationship to that read).
In summary, you definitely need some way to ensure a happens-before
relationship going from your writes to your reads. Which exactly depends on the particulars of your code.
Upvotes: 4
Reputation: 160190
Sure–any time multiple threads can access the same data there's a potential for a problem.
A user can open multiple tabs/windows and make near-simultaneous requests. Those requests will be processed in a non-deterministic amount of time and sequence.
Upvotes: 6