• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

how are local variables in doPost() thread safe?

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose you have the following code in doPost():



Obviously, the user agent can be different for a different client request. So how can you count on the output being correct? Couldn't another client make another request which starts another thread, and then couldn't the second thread set the output to something else before the output in the first thread can be displayed?
 
Sheriff
Posts: 67695
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each request, running in its own thread, gets its own HttpServletRequest and HttpServletResponse instances and output buffer. You don't need to worry about them overlapping.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
Each request, running in its own thread, gets its own HttpServletRequest and HttpServletResponse instances and output buffer. You don't need to worry about them overlapping.



Yes, I knew that part. Let me see if this is a better example of what is confusing me:

int x = 20;
x = 10;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<div>" + x + "</div>");



How do you know a 2nd thread won't be assigning 20 to x, just before this thread adds the value of x to the response? I don't see how setting x = 10 is isolated inside one particular request; it's inside the servlet's doPost() method, which all the threads access.
[ October 10, 2006: Message edited by: sven studde ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There will be one copy of x for each request.

In contrast, an instance variable would not be thread safe becuase all requests will use the same one:

Local variable (thread safe):


Instance variable (not thread safe):
 
Bear Bibeault
Sheriff
Posts: 67695
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add to Ben's excellent post, that's the way Java threading works. Variables that are local to a method are allocated on a per-thread basis. You never have to worry abouth thread-safety with variables declared inside a method.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I was hoping to be enlightened with a rule that when applied to instance variables and servlet context attributes resulted in them being not thread safe, yet when the rule was applied to local service method variables it resulted in those variables being thread safe. But if the rule is "that's the way it is", then I guess it's just memorization, and there's no logic behind it.
[ October 10, 2006: Message edited by: sven studde ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But if the rule is just "that's the way it is", then I guess it's just memorization.


I don't understand what you have to memorize here. A bit of logic will do. As Bear said, that's how threading works.
 
Bear Bibeault
Sheriff
Posts: 67695
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

... yet when the rule was applied to local service method variables it resulted in those variables being thread safe



How much more detailed of a rule than "Variables that are local to a method are allocated on a per-thread basis" do you need?
[ October 10, 2006: Message edited by: Bear Bibeault ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic