• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Making Servlet Thread Safe

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 42073
932
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
knowledge is the difference between drudgery and strategic action -- tiny ad
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic