Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
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
 
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() ?
 
Rancher
Posts: 43016
76
  • 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.
 
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
Posts: 43016
76
  • 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
Posts: 43016
76
  • 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.
 
It is no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic