Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in servlet

 
raja ram
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consider the example below

public class a extends HttpServlet{

protected LinkedList l=new LinkedList();

public void init(ServletConfig config){

for(int i=0;i<10;i++)
l.add(i);
}

public void doget() throws ServletExcetion,IOException{}

}


in this case the protected variable l were we are making add operation in init() will these not affect when multiple thread are executing together

say thread-1 will create the object and will be doing add operation.
in this time if thread-2 instantiates variable l will it not affetc the state of thread-1.

according to me it should affect both the threads but i am not able to find any difference when i execute the servlet it runs with out any problem.


for me code is working with out any problem even when i submit multiple requests.

I know init() will be executed only once say for the first request only that time i will built the list of items. and in dopost() i will only read it.

This will work fine for first thread. say now i will submit second request means a second thread now it will defnitely re instantiate my LinkedList l
and init() will not execute this time even though i am only accessing the elements from LinkedList l it should not have previous request value that it got populted for the very first request when my init methode got executed. am i correct.

but in my case it works fine with out any problems.and displays values that got populated when init() got executed

what i guess even though i have LinkedList l declared as instance variable but i am building it only once in the very first request through init() servlet will save the state of the object some where and later for other requests even though it instantaties the LinkedList l when executing dopost() at the time of accessing it will use the one it as already stored when it executed init() and not the one it newly instantiated for this thread.

is there any concept like this,can any one throw some more light on this
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by raja ram:
...say thread-1 will create the object and will be doing add operation.
in this time if thread-2 instantiates variable l will it not affetc the state of thread-1.

...I know init() will be executed only once say for the first request only that time i will built the list of items. and in dopost() i will only read it.

...This will work fine for first thread. say now i will submit second request means a second thread now it will defnitely re instantiate my LinkedList

Whoa whoa whoa! The container reuses the same Servlet object. No object (Servlets included) would ever reinstantiate its variables simply because a new thread is calling methods on it.

Your Servlet variable is thread safe because the only method that alters it is the init() method, which only runs at the start of the Servlet's lifecycle. If you were to alter the list in a doGet, doPost, or similar method, then your variable would no longer be thread safe.
 
raja ram
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply, when we say no object will be reinstantiated that means even the line LinkedList l=new LinkedList() will not be exceuted for the next request is that correct.

because instance variables will be exceuted for every instance creation but in case of servlets the container will instantiate the servlet only once for the request so the instance variables l will be instantieated only once and for all the other request it will simply use the same as told in the above comment is my understanding correct.

Thanks
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the line LinkedList l=new LinkedList() will not be exceuted for the next request

Correct.

because instance variables will be exceuted for every instance creation

Correct.

in case of servlets the container will instantiate the servlet only once for the request

No. The servlet (like any other object) is instantiated exactly once. Nothing is instantiated (or initialized or re-initialized) for each request. The doGet (or doPost) method is called for handling a request, that's all.
[ March 12, 2008: Message edited by: Ulf Dittmer ]
 
Gunnar Maluf
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, i just read that every request runs in a separate thread even if it comes from the same client. Is it wrong?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
every request runs in a separate thread even if it comes from the same client. Is it wrong?

No, that's correct.
 
raja ram
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply i understood the concept.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic