aspose file tools
The moose likes Servlets and the fly likes Thread safety again/ThreadLocal viability Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Thread safety again/ThreadLocal viability" Watch "Thread safety again/ThreadLocal viability" New topic
Author

Thread safety again/ThreadLocal viability

Graham VMead
Ranch Hand

Joined: Sep 22, 2003
Posts: 154
We are currently writing a system which parses data into PDF output via Apaches FOP. This is done in a Servlet which needs to load data into variables and whoses process takes a reasonable amount of time i.e 5-10 seconds. This data is accessed by many of the methods used in the servlet.

The system can be used concurrently by many users.

Currently to preserve threadsafety a local instance container object (holding the data) is created and passed down into the various methods.

This can be a bit of a pain and I was looking at alternatives.

Is putting the container into a ThreadLocal class a viable alternative, it seems so IBM Example but then I've also seen article about crummy performance and also multiple threads per request???

Is there any other approach out there, apart from SingleThreadModel or synchronization. Effectively I want to access data specific to a request without the overhead of passing local variables all over the place?

TIA Graham
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Your servlet can do something as simple as this:

WorkerObject has no thread issues because you make a whole new one for every thread. You may decide you have object creation and GC issues instead, but you've already explored the alternative of all local variables and parameters so you can compare the pain and benefits.

This is more or less what Struts and other front controller designs do when they pass work off to action classes and such. Unless they use singleton action classes that have all the same thread issues as the darned servlet. :roll:
[ December 13, 2004: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Graham VMead
Ranch Hand

Joined: Sep 22, 2003
Posts: 154
Cheers Stan,

Why didn't I think of that simple yet ideal for what I'm doing. I'm off to refactor now!

Graham
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Thread safety again/ThreadLocal viability
 
Similar Threads
Advantage of Threadlocal over session object
What is ThreadLocal
With global variables, threads, and a really sore head! :)
Thread Safe? Request Attributes and Local Variable
regarding SLSB and ThreadLocals