Forums Register Login

threading issue in servlet

+Pie Number of slices to send: Send
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??
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
can you give any example that make it to me more understandable??
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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....
+Pie Number of slices to send: Send
I think it'd be even more instructive if you took my description and created your own example that you can run and test.
+Pie Number of slices to send: Send
okay, i will do it....
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
 

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??
+Pie Number of slices to send: Send
You don't need to implement any threads. Why do you think that you do?
+Pie Number of slices to send: Send
 

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??
+Pie Number of slices to send: Send
I ask again: why are you even thinking of implementing threads in the servlet?
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
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.)
+Pie Number of slices to send: Send
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??
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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??
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
The example I described used instance variables. That's almost always a problem.
+Pie Number of slices to send: Send
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...??
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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...
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks for more clarification Bibeault
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1578 times.
Similar Threads
actuval definition of servlet
servlet
replace String from a file
How can i get Servlet RI source code?
Temporary Servlet
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:41:53.