1

I am working with a Java web application backend. We have a Servlet that in the init() method has

public static final ThreadLocal<HttpServletRequest> REQUEST_HOLDER = new ThreadLocal<HttpServletRequest>();

In the service() method

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException

the Servlet then sets the value of REQUEST_HOLDER with the request.

REQUEST_HOLDER.set(request);

Later in the servlet, I want to do some heavy processing on another thread, but still reference the request from the ThreadLocal REQUEST_HOLDER, which is technically on a different thread. Is this possible? how? (new to multithreading)

Although this general concept has largely been addressed in ThreadLocal value access across different threads , I want to know if there is anything unique regarding servlets / http requests that might provide a different solution. If not, not.

Also is it possible to take the REQUEST_HOLDER ThreadLocal and override the get() method so that if it is null (as in when called from a different thread) it returns a different value? - thinking of storing the request somewhere else and have the get() return that value.

Community
  • 1
  • 1
Binyomin
  • 31
  • 6
  • 1
    Possible duplicate of [ThreadLocal value access across different threads](http://stackoverflow.com/questions/5180114/threadlocal-value-access-across-different-threads) – Alex Shesterov Mar 20 '17 at 21:53
  • 1
    On face value, it defeats the purpose of thread local. why not share the meaningful data? – bichito Mar 20 '17 at 22:00
  • @efekctive good question. This was so that the request can be accessed easily from other files without passing around the id. In the file (let's call it ServletHome.java) that extends HttpServlet we have this static ThreadLocal. Then in other files we just call ServletHome.REQUEST_HOLDER.get() This has been working so far, since we haven't attempting to do any multithreaded work that relied on accessing that original request with ServletHome.REQUEST_HOLDER.get(). I am getting the feeling that I am looking to do the impossible – Binyomin Mar 21 '17 at 14:21

3 Answers3

1

Another solution instead of messing with the ThreadLocal mechanism would be to extract all necessary data from the request object in the main thread, put it into a separate data object, and then give this object to the new thread.

dunni
  • 43,386
  • 10
  • 104
  • 99
  • This is definitely the route I would have liked, however there are so many static methods in place already that reference the request in that thread local. I need to call these static methods, and it is not feasible (it is possible but not realistic given the time requirements) to change every one of them to take new parameters to make this work. – Binyomin Mar 21 '17 at 13:52
  • Then you can just pass that data object to the other thread while keeping the ThreadLocal and anything that depend upon it as-is. – glee8e Mar 21 '17 at 14:40
1

A coworker suggested to me that when I start the new Thread, I should pass in the request. Then in the new thread I can set the ThreadLocal REQUEST_HOLDER with the request from the new thread. Thanks all for your answers

Binyomin
  • 31
  • 6
0

As per the Java documentation, ThreadLocal variables are initialized so that they are only visible to individual threads.

Of course any value can be shared, so one could just make the Object value returned by the ThreadLocal get() method accessible to other threads, but that defeats the purpose of having such a variable in the first place.

PNS
  • 19,295
  • 32
  • 96
  • 143