Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variable

 
rex tony
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static variable Is Thread Safe?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, static variables are not automatically thread-safe. You'll have to take care of synchronization if you have multiple threads that are accessing a static variable simultaneously.
 
camelia codarcea
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Static variable should be changed inside static syncronised methods in order to be thread-safe.
Two threads that access a static syncronized method, will always block each other, regardless if they are using the same instance or two different instances.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic