• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

threading issue in servlet

 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Sheriff
Posts: 67756
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
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.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you give any example that make it to me more understandable??
 
Bear Bibeault
Sheriff
Posts: 67756
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
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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67756
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
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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, i will do it....
 
Bear Bibeault
Sheriff
Posts: 67756
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
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67756
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
You don't need to implement any threads. Why do you think that you do?
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67756
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
I ask again: why are you even thinking of implementing threads in the servlet?
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67756
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
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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67756
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
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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67756
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
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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67756
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
The example I described used instance variables. That's almost always a problem.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...??
 
Ranch Hand
Posts: 171
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 171
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67756
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
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
Posts: 171
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for more clarification Bibeault
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic