• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static variable  RSS feed

 
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
Sheriff
Posts: 15865
81
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!