• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread safety again/ThreadLocal viability

 
Graham VMead
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Graham VMead
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers Stan,

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

Graham
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!