aspose file tools*
The moose likes Servlets and the fly likes threading issue in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "threading issue in servlet" Watch "threading issue in servlet" New topic
Author

threading issue in servlet

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 931
can anyone tel me what is the problem of threads in servlet, and how servlets are not thread safe??
i search on google, but not getting exactly, what is it??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

The problem isn't that servlets are not thread-safe, the problem is that you, as the writer of a servlet, must write the servlet in a thread-safe manner. A servlet isn't non-thread-safe until you make it so.

Because an instance of any servlet can be used by multiple threads at the same time, the servlet must not do anything that causes data to be accidentally shared by multiple threads. Avoiding member variables is a good first step.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 931
can you give any example that make it to me more understandable??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Take the example of looking up something in a database for the user.

And let's say that a poorly-written servlet calls some model method that returns the value from the database. So one thread running the servlet looks up the value and stores it. Another thread looks up a different value (for a different user) and also stores it. What happened to the first value? It's gone because the single servlet and it's single instance variable can only store one value at a time. So the first thread has had its data corrupted and could end up showing one user the data for another because the servlet was not thread-safe.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 931
okay, therefore we should avoid member variables to make a servlet thread safe??
one more thing if possible can you give me any example that i can run on my system and help me understand it more..

thanks for a good explanation....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

I think it'd be even more instructive if you took my description and created your own example that you can run and test.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 931
okay, i will do it....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 931
Bear Bibeault wrote:


i m trying to do it, i write a servlet to fetch some data from database, not not getting exactly how to implement thread??
here is what i written:



can you please help me in this, and tell me what next i need to do??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

You don't need to implement any threads. Why do you think that you do?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 931
Bear Bibeault wrote:You don't need to implement any threads. Why do you think that you do?


what i think is that if i implement Thread methods(ie.Thread.sleep() etc), they are not easy to use in servlets, isn't it?
or i m again doing any mistake??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

I ask again: why are you even thinking of implementing threads in the servlet?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 931
okay, when we execute servlets it one execution is considered as one thread itself, isn't it??
and what i was thinking is that, if i want to understand why servlets are not thread safe, i need to implement threads into my servlets.

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 931
i mean each client is considered as one thread in servlets, i mean when i run my servlet from one system it's one thread and when i run that servlet from another system, it will be considerd as another thread,
this is role of thread in servlets i think, isn't it??
and i was going wrong i donot need to implement thread into my servlet, because each request is considered as one thread, but as far as my code 'which is fetching data from database' is considered how is it thread safe or not,
if yes than how and if not than how??

i again apologies if i m going wrong, please correct me..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

The container handles creating the threads for you. You do not need to do anything yourself in the servlet except to write it in a thread-safe manner. (E.g. avoid read/write instance variables, synchronize access to shared resources, etc.)
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 931
well, okay i got it, but the next question comes in my mind is how do i know weather my servlet is thread safe or not??
i mean the code i post earlier in this thread, how do i know is that thread safe or not??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Think about it. Thread safety issues come about when two threads can stomp on each others' state. If all you are using are automatic variables (those declared within a method), each thread has its own copy of those variables so there's no problem.

Do you access outside resources? The DB obviously. But DB's handle their own threading so not an issue.

It's a thought exercise you need to keep in mind as you write your servlet code.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 931
humm, it's confusing me i think, okay DB handles their own thread, but in the earlier post you gave an example


Take the example of looking up something in a database for the user.

And let's say that a poorly-written servlet calls some model method that returns the value from the database. So one thread running the servlet looks up the value and stores it. Another thread looks up a different value (for a different user) and also stores it. What happened to the first value? It's gone because the single servlet and it's single instance variable can only store one value at a time. So the first thread has had its data corrupted and could end up showing one user the data for another because the servlet was not thread-safe.


so if there is no issue of threads in db than how this example can cause a thread problem??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Who said that it did?

The only problem I see is that it leaks database resource like a sieve; but that's not a threading issue.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 931
you gave that example here, (and thread title is "threading issue in servlets" offcourse) therefore i thought it supposed to be cause a threading problem,
therefore i post int his thread how to recognize whether the code is causing any threading issue or not, because i am unable to recognize..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

The example I described used instance variables. That's almost always a problem.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 931
okay in your described example, you are declaring variable in class, that will definitely create a problem, because each instance of the class has a separate copy of these variables, so the value will not be updated each time, isn't it??
so to make this thread safe, i should declare these variables in member function...??
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

How do you say it would create a problem? Each thread should have a separate copy of variables defined in a separate class/inner class to avoid sharing. That's the requirement right !. If you are going to declare it in member function then you can't use them outside and may need to pass more args.


No pain, No gain.
OCJP 1.6
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

In real time, I've experienced the thread sharing problem and fixed through inner class in which all the required member variables are declared. It's working fine.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 931
How do you say it would create a problem


i said this because we mostly avoid the class variables to make a servlet thread safe, i think...
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

Class variable in the sense you have to create a separate/inner class to initialize variables, and which has to be called from the servlet for every request/process. See below example.



So every request comes into the servlet will make a call to ClassVariables type to initialize and would have it's own copy of variables. So definitely it will avoid thread sharing of member variables.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

The term "class variables" means variables that are declared at class level; it does not mean inner classes.

Read/write class variables are avoided in servlets because they are shared amongst all threads. Inner classes are not necessary for thread safety. Declaring variables within the methods keeps those variables thread-safe as each thread will have their own copies.

If information needs to be shared beyond a method invocation, the session and application scoped can be used.
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

Thanks for more clarification Bibeault
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: threading issue in servlet
 
Similar Threads
actuval definition of servlet
Temporary Servlet
replace String from a file
servlet
How can i get Servlet RI source code?