can anybody help me in this issue..as I'm new to Servlets.
How the Servlets are providing the thread-safety feature, even though it can be accessed by multiple threads at a time and even SingleThreadedModel concept has been removed ?
Servlet is not Thread safe, so it's all about programming, if we want to make servlet as thread safe the use the synchronize block to synchronize only part of the code which you intended inside the doGet or doPost methods.
Bear Bibeault
Author and opinionated walrus
Marshal
tinku rider wrote:if we want to make servlet as thread safe the use the synchronize block to synchronize only part of the code which you intended inside the doGet or doPost methods.
No, that's a bad idea and something that's only done as a last resort.
Rather, most servlets can be made thread0safe by simply being sure not to use any instance variables.
Avoiding the use of instance variables solves 99% of all threading issues. In over 10 years of writing servlets I've never had to use a synch bock.
somasundar venkatesh
Greenhorn
Joined: Mar 01, 2011
Posts: 14
posted
0
Bear Bibeault wrote:Because it causes more trouble that it solves.
Avoiding the use of instance variables solves 99% of all threading issues. In over 10 years of writing servlets I've never had to use a synch bock.
Hi Bear, got it now. Thanks for your reply and thanks for all who have replied.
Regards,
somu
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2317
posted
0
somasundar venkatesh wrote:But if we use explicit synchronization then again that concept is same as SimpleThreaded Model right.
No. Synchronization can be used for code blocks as small as you like, whereas STM effectively synchronizes the entire code of a servlet by ensuring that there is no concurrency.
STM was removed because it does not solve all concurrency issues as people seemed to assume, and -even worse- completely killed concurrency; that's not what you want in a web app. There is no shortcut to learning about the multiple concurrency options in Java if you're serious about web apps. A book like "Java Threads" or "Concurrency in Practice" will help tremendously with that.
Punit Jain
Ranch Hand
Joined: Aug 20, 2011
Posts: 694
posted
0
i read this thread, but i want to know what exactly is the problem of thread in servlet?
is it mean that if i use thread methods in servlet, they are not safe?
isn't it??