Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static methods and thread safety

 
Swapnil Dharane
Ranch Hand
Posts: 55
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
Can you please tell me whether static mehods/variables are thread safe? if not why not?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing about static methods or variables that makes them inherently thread-safe.

You can easily write some example code with two threads, in which the threads try to write to the same static variable at the same time, so that the end result is unpredictable - which demonstrates that it's not thread-safe.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic