• 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

Making Servlet Thread Safe

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

I have one doubt. Here is simple code which is a servlet.



As to make this code thread safe, do we need to make the increamentValue() method synchronized or do we need to use a Synchronized block in doPost() method. I believe that making increamentValue() method as synchronized will not make it thread safe. We have to use Synchronized block in doPost() method. Please let me know i am correct or not.

In a book, it is saying the below code is Thread safe but I think it is not thread safe. Please correct me.



I just want to say, making the incrementValue() method synchronized is not enough to make it thread safe.

Regards,
M.Sufiyan
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is thread safe, since only one thread is able to alter the value of the shared state (ie the 'x' instance variable).
It just isn't a very good way to achieve thread safety.
 
Mohammad Sufiyan Al Yousufi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David O'Meara wrote:it is thread safe, since only one thread is able to alter the value of the shared state (ie the 'x' instance variable).
It just isn't a very good way to achieve thread safety.



I think, it seems thread safe but it is not. I may be wrong.. Lets say, two thread accessing the below code..



Thread-1 while executing the increamentValue() method, which will be sequential. After making changes to x variable. And while Thread-1 executing Line B, it may happen that Thread-2 executes the incrementValue() method and While Thread-1 executing Line C, it gets wrong output. This is the only doubt I have, to make this program thread safe, I think, we should use Synchronized block in code like..



Am I correct?
 
author & internet detective
Posts: 40198
816
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is thread safe because thread safety refers to state. In particular, the state of variable x. Printing out an old value isn't part of thread safety. It also has less meaning. By the time the response streams back to the browser, it could be out of date anyway.
 
It's exactly the same and completely different as this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic