This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Slap me silly, but...

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question may seem very silly, but bare with me please. In doGet or doPost method in a servlet that doesn't implement SingThreadModel, I instanciate an object called "MyObject".
1: public void doGet(...)
2: {
3: MyObject mo = new MyObject();
4: ...
5: //using set methods in MyObject, mo is
6: //customerized for each users that call
7: //this servlet
8: process(mo);
10: }
11:
12: private void process(MyObject mo)
13: {
14: //do something with mo
15: }
Let's say a split second later first request came in second request came in to the doGet method. By the time the first request is on the line 8, how do I know that the second request didn't changed the mo object? How do I make sure that the integrity of mo is kept? I read from somewhere that local variables and the parameters are threadsafe, but I just can't seem to understand it. Can someone explain what really happens or how it should be coded?
[ August 19, 2002: Message edited by: Servin Park ]
[ August 19, 2002: Message edited by: Servin Park ]
 
Ranch Hand
Posts: 8946
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
For every request a new object will be created in the doGet method. So the objects are thread safe
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This servlet does NOT implement the singleThreadModel interface. So a new object wil not be created for every request. A new thread will be spawn for the same instance. If you want to guard against unfavourable results since all the threads will be sharing the same instance variables, you can either implement the SingleThreadModel interface, or synchronize access to the code in question.
 
Sheriff
Posts: 67754
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
While instance variables are certainly not safe, the local variables in doGet() and doPost() are completely thread-safe.
Instance variables are created at construction-time of the servlet and only one variable per instance is created. That is why they are not safe: if multiple threads use the same object, the instance variable is shared and can get clobbered.
However, local variables within a method are created on the method's stack and are private to the invocation of that method. Thus, even if the servlet instance is shared across multiple threads, each method invocation has a private copy of its local variables and no thread-safety issues are created.
hth,
bear
[ August 20, 2002: Message edited by: Bear Bibeault ]
 
Servin Park
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for your kind suggestions/comments.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic