• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlets and Threads

 
Simon Harvey
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bon Journo Meo Pusssy Caaaats,
Hope everyone is well. I was wondering if someone could help me with the thread model that servlets use. I understand that multiple threads can execute accross the same code and thus interfere with one another. I was wondering though, apart from the SingleThreadMethod, is the only other way to deal with this to synchronise methods that change variables?
Its just that that would probably be almost every thread serialised surely? I mean, how many methods do you write that dont modify variables?
If I'm missing something then it would be great if someone could explain why, or alternatively, are there any design strategies that make things easier for us.
Thanks everyone
Take care
Simon
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I'm anyone's pussy cat, but I think that sounded friendly enough, so good morning!
Where you're really restricted is in member variables. The only appropriate member variables are those that will be shared by all threads. I recently made a servlet in the mock tradition that does nothing but count how many times it is called from a volume test of another system. It has a member variable counter with synchronized code blocks to add 1, reset, and display the current value.
You are ok to work with your parameters, any local variables and anything you've stuck on the session. One thing you see is a lot of parameter passing. If I have a lot of little methods that can write snippets of HTML, I'll pass the response writer around. Kind of ugly.
Another technique is to create a new instance of some plain old Java class and delegate all the work to it. This offsets all the advantage the servlet container realized by reusing servlets to minimize object creation and gc.
Yet another is what Kent Beck calls a Collecter argument. It's a data-only object that you pass to many methods so they can get and put data. Use this data kinda like you'd use member variables.
Other ideas in this space?
 
Simon Harvey
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bon Journo meo funky monkey cockatiles,
Thanks for the reply Stan. I'm afraid I dont really follow what you're meaning with the parameter passing and stuff. What does it all have to do with threads and stuff? Sorry, I never have been able to get this stuff. Thats why I'm trying again
Heres an example of the sort of thing that I'm concerned about:
Suppose I have a highly ficticuous servlet responsible for completeling an ecommerce order. The part that I'll talk about is when the servlet is writing an order summary to the screen.
Lets say for a moment that the customers details are still in the session and for whatever reason, have not been assigned to local variables (come to think of it, should they be assigned to local variables?)


OK, Thats nice and simple. Buts to me, this seems like a very typical type of method that could get into trouble with multiple threads trampling over it. What if a context switch happend right after doing the username println?
Surely it could go wrong?
I'm not sure if what you were saying before was that sometimes you are just inherently protected from multithreading issues. But I dont think that can be right?
But then it seems that a great many methods would just have to be one thread only. But surely that cant be right either?
Arrghh. Whats going one!?
Thanks for any more help anyone can offer
Take care all
Simon
 
Steve Chernyak
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables declared inside a method are not shared between threads.

While you are doing stuff with session there is no way for another thread to use that session. The values get switched with the context when another thread runs.
This however would lead to promlems: (NEVER DO THIS)

Does that make more sense?
 
Simon Harvey
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve,
Well put. So does this mean I can stop panicking about threads and just program as long as: I dont make global variables that pertain to individual clients?
Ok, so, in what circumstances do i need to synchronise a method? Is it only when i try access a global variable?
What about accessing a database via a helper class
Thanks very much for your help guys
Simon
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!