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

Static and Non static Synchronized methods

 
Kishore Kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
CAn any one let me know, Is there any difference between static and non-static synchronized methods? If yes, what is the difference?
 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes static synchronized methods lock on the class and instance methods lock on the object
 
Kishore Kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. Then suppose there are 2 threads. Thread-1 is accessing static synchronizes method. Now can thread-2 access non-static synchronized method if thread-1 is accessing staic synchronized method.
 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes if Thread-1 is accessing static synchronized methods of a class then Thread-2 can access the non-static synchronized methods of that class... the point is static synchronized methods and non-static synchronized methods dont block each other
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way of looking at it is that the "synchronized" modifier is essentially just syntactic sugar for these two constructs:

"static synchronized void foo()" is behaviorally equivalent to:

"synchronized void foo()" is behaviorally equivalent to:

[ November 05, 2007: Message edited by: Kelvin Lim ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic