Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
raja pulleboina
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to synchronize service() in servlets with out using singlethreadmodal interface....> thanks inadvance
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to do these ?

Do you want to synchronize only a part of code in Service() ?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
swapnl patil
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do you want to do this ?

you can do this like this
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic