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??
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.
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.
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..
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??
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.
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..
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.)
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??
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.
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??
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..
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...??
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.
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.
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.
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.