2

I got a doubt about threadlocal usage in managing sessions. It is..

In Thread Local, which ever thread that creates the thread local object has the access to that session object, and only that thread can modify the session object data. There might be many threads running to complete a single user request. So what about all other threads that participate to complete one user request.

They wont get access to modify the session object, as whichever thread creates the Thread local object gets its own session object, so other threads that run to complete a single user request might not update their data to the session object to which they actually wanted to.

I mean if thread-1 and thread-2 participate in completing a user request, but thread-1 comes to create the threadlocal object, and thread-2 executes other codes as part of the request, but after thread-2 completion , it cant update the session data because, only tharead-1 has access to the session as it has created the threadlocal object.

So how are we resolving this issue.

Are we making sure that only one thread participate in completeing single users request ? or

How are we so sure that which ever thread that creates the threadlocal object is only coming to update the session data associated for that request ?

santhosh
  • 21
  • 1
  • 3

1 Answers1

3

Well first, RTS (Real-time system) have to be distinguished from high performance systems. In RTS time has been split in frames and piece of software have one or more frame allocated to them. Which makes very predictable which task system is performing at a given time and also which other tasks are running in parallel. Thus design help in avoiding/limiting concurrency management.

Second, high performance systems (whatever RTS or not) don't necessarly rely on multithreading for the exact same reason as for RTS : concurrency management and blocking structures more precisely are bottlenecks. And multithreading is not the only solution for parallel processing. Forks, grid computing and forth work greats and offer more scalability, stability, robustness, etc.

So, single thread environments are generally based on an async design. You split your work (ie request processing) in small non-waiting/blocking structures. All waiting/blocking (or even agressive long computing) are sent "somewhere" and just notify request processor when complete. In this case one thread can treat many request during a single request processing (not sure to be clear at this point ...)

In such (and some other) cases, you can't use thread-local binding. However, you can use "local scope" to share a reference across your processing :

Map<String, String> context = new LinkedHashMap<>();
context.put("id", "42");
taskQueue.add(() -> context.put("state", "action1"));
taskQueue.add(() -> System.out.printf("%-10s > state=%s%n", context.get("id"), context.get("state")));

(assuming taskQueue is Queue of Runnable)

You can also save request context by:

  1. generating a "Unique Request Identifier" (suitable class should be UUID)
  2. using a request context storage (can be a simple ConcurrentMap) or more advanced storage such as Key-Value Cache, Key-Value Store or Document store.
  3. attaching "Unique Request Identifier" to all request-bound actions
LoganMzz
  • 1,597
  • 3
  • 18
  • 31
  • It's already answered. Don't use thread-bound but request-bound storage. It can't be done in a local scope way such as in my code example or using a request identifier and a storage API such as explain in my edit (last paragraph after code example). Let me what additional details you need to be satisfied. – LoganMzz Oct 13 '15 at 14:58