This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes how to synchronize service() in servlets with out using singlethreadmodal interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to synchronize service() in servlets with out using singlethreadmodal interface" Watch "how to synchronize service() in servlets with out using singlethreadmodal interface" New topic
Author

how to synchronize service() in servlets with out using singlethreadmodal interface

raja pulleboina
Greenhorn

Joined: Dec 10, 2009
Posts: 12
how to synchronize service() in servlets with out using singlethreadmodal interface....> thanks inadvance
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

Why do you want to do these ?

Do you want to synchronize only a part of code in Service() ?


- Chinna
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42374
    
  64
For starters, leave the service method alone. It's extremely unlikely that overriding it is the right thing to do, and much more likely that it's the wrong thing.

Instead, synchronize individual code sections in the doGet and/or doPost methods, making sure to keep those sections as short as possible. You may also want to look into the numerous other concurrency features Java has (ThreadLocal, volatile, java.util.concurrent, ...) that can prevent bad stuff from happening without resorting to explicit synchronization.


Ping & DNS - my free Android networking tools app
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
why do you want to do this ?

you can do this like this
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42374
    
  64
swapnl patil wrote:
you can do this like this
...

I'm sorry, but that's really bad. Firstly, it overrides the service method, which is the wrong thing to do. Secondly, it synchronizes the entire method, thereby completely destroying concurrency.
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
Yeah I know that. But I don't know why this person want to do this.
In real world we place critical code in synchronized block.
raja pulleboina
Greenhorn

Joined: Dec 10, 2009
Posts: 12
what happens if I do write synchronized block in service method... does it throws any exception or error..please explain..thanks
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
It will not throw any error or exception,but if you make service method synchronized or used synchronized block for all code in Service method then you mess up the things. Only One thread will access service method and other need to wait. Its like you are implementing single thread model. Instead of this use synchronized block for critical code.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42374
    
  64
raja pulleboina wrote:what happens if I do write synchronized block in service method... does it throws any exception or error..

Again, do NOT override the service method; override the doGet and/or doPost method method instead.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to synchronize service() in servlets with out using singlethreadmodal interface