Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Misunderstanding  RSS feed

 
Ryan Bailey
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, can someone explain this better? Check out this code below.

Here are the instructions
1 the servlet container assigns a worker thread (for example, T1) to service A's request and another worker thread(t2) to service B's request.
2. The OS schedules T1 to run.
3. T1 extracts the name parameter from the request and saves it in the username variable. The value of username is now A.
4. Just before T1 attempts to execute the next statement, the OS preempts T1 and schedules T2 to run.
5. T2 extracts the name parameter from the request and saves it in the uername variable. Therefore, the value of username is now B. Notice that the value saved by T1 is now lost.
6. T2 executes the last two lines and a valid response displays, "Welcome, B" is sent to user B
7> Now, ths OS schedules T1 again, which starts from where it was preempted. It executes the remaining two lines. Since the vlaue of the username vaiable is B, it sends "Welcome, B" to user A.
Does anyone know how often this might occur? So this means I should define all my variables as local(ie. inside a method)? I though that as long as the variable username isn't defined as static I shoudln't have to worry about another request obtaining a different value. I guess in this example both A and B are referring to the same instance?
So when I'm writing a servlet not only do I need to worry about the container creating multiple instances of a servlet, but for the 'thread dispatcher' dispatching multiple threads(workers) running the same instance??
AHHHHGGG!
Any help on this would be greatly appriciated. This example was taken from a Java Web Componet Developer Certification book.

Thanks in advance for your time,
Ryan
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66143
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, since the servlet can be executed by more than one thread at a time, any data that is specific to the current request must be thread-safe. That means, in local variables, or tucked into the request instance via setAttribute()/getAttribute().
The only instance variables (static or otherwise) a servlet should have are those that will be shared across all threads (with appropriate synchronization for read/write values).
It's really not that tough. I've been writing servlets for quite some time now and I don't find it onerous to adhere to the multi-threaded rules. Most data that you deal with during servlet processing is specific to the current request and it's no big deal to treat it as such.
Did this address your concerns?
hth,
bear
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this means I should define all my variables as local(ie. inside a method)?
Yes, unless the variable references a resource that needs to be accessed by anybody accessing your servlet, and even then you might have to synchronize access to it. The general rule of thumb is to keep your variables local.
I though that as long as the variable username isn't defined as static I shoudln't have to worry about another request obtaining a different value. I guess in this example both A and B are referring to the same instance?
Yes both are referring to the same instance. Go back and re-read about the servlet lifecycle. Only a single instance of your servlet is created by the container, with each user request resulting in a new thread (unless you've implemented the SingleThreadModel interface).
So when I'm writing a servlet not only do I need to worry about the container creating multiple instances of a servlet
In most all circumstances your servlet container will only create one instance of a servlet.
 
Ryan Bailey
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did this address your concerns?

Yes, thank you very much. I was recently working with AppLogics (You ever hear of them?) and must of gotten that technology confused with the Servlet Framework. I guess if I need to share a variable in multipe functions for a servlet I'll either create an object to be passed around or just pass my variables in a method. Is there an easy way to accomplish this without defining an instance variable(since that will not work)?
In this particular case, could someone tell me what the difference is between defining a variable as an instance variable as opposed to a class variable? I realize that a class variable there is only one copy no matter how many instances of the class. Is there any other differences?
Cheers ,
Ryan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!