Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

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: 40239
819
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.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic