This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Thread safety question

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I search the forum and on the internet, but seems there is no thread answering my doubt. So I would like to post a new thread to make clear.

I have a servlet, which in turns call another class (e.g. class X{...} ), in which there is a class variable (e.g. static Map map = Collections.synchornizedMap(..)). In servlet, all variables are local (inside doGet/ doPost) and class variable (e.g. static class ...{} ). But in the class X, it has instance method (e.g. doSomething()) where it would operate actions similar to `if put absent' scenario. For instance,



Consider this scenario. the method doSomething() inside class X will be thread safe?

Thanks for help.
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nopes does not look thread safe to me. In particular,



One thread could get the value from the map. Context switch could occur at this point. Second thread gets the same value from the map instead of the value stored back by the first thread....
 
jason williams
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.

Does that mean even if this class is used as a local variable inside a servlet, it is still not thread safe? That is what me confused because I read some document on the internet saying that a local variable is always thread safe. For instance.




Thanks again for your help.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if you create the local variable (object) of the class X, every thread will have its own object of class X sharing the same static member of class X (i.e. map). So its not thread safe.
 
jason williams
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fairoz Shaikh wrote:Even if you create the local variable (object) of the class X, every thread will have its own object of class X sharing the same static member of class X (i.e. map). So its not thread safe.



Thanks for the explain. If the variable e.g. map is not declared as static (which would be referenced by all other threads running in the servlet in that case) in class X, and class X in the servlet is defined as local variable (e.g. X x = new X(); in doPost method ), does that mean it is thread safe? (In this case, the map if not declared as static, the result may go wrong; but just want to make clear about the issue of thread safety. )



Thanks again for all your help.
I appreciate it.


 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will be thread safe in that case.

One more information: variables can not be marked synchronized.
 
Ranch Hand
Posts: 80
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still new at this and others can correct me if I'm wrong but if you made your
map variable private static and in any of the methods that change it's value
make those methods synchronized then it should be thread safe...

Maybe?
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To access static variables from methods, those methods should be static. In that case, you need to synchronize on the Class.
 
Marc Cracco
Ranch Hand
Posts: 80
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Might sound like a beginner question but why shouldn't you access a static variable from a local synchronized method?
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marc Cracco wrote:Might sound like a beginner question but why shouldn't you access a static variable from a local synchronized method?



Sorry if my previous post confused you. You are right, static variables can be accessed in local synchronized methods. But the lock should be on the Class again or some other static lock object. That is what i wanted to say in my previous post; but framed the sentence poorly
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic