• Post Reply Bookmark Topic Watch Topic
  • New Topic

ques on threading  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet where I hava a instance variable int i =100; and inside the service method I change the value of i at two places in the service method
service()
int i=100;
;
;
;
i=200;
;
;
;
i =300;
Now I am not implementing SingleThreadModel.
So that means I have 1 servlet instance. Say there are 3 requests coming at a difference of a sec. SO for each req there is going to be a thread. Am I corect so far ?
I want each requ to print 300 as he value of i.
is this possible. if yes how does the server do this and if no..what should I do to make it print 300
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i is an instance variable, its value when the print statement is reached will depend on where the most recent thread to visit the service method left it. So it may have any of those values.
What exactly are you trying to accomplish anyway???
Bill
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
There is no way you can guarantee the exact state of a servlet instance variable in a multi-threaded environment.
-anthony
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!