• Post Reply Bookmark Topic Watch Topic
  • New Topic

static synchronize  RSS feed

 
Atul Shukla
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! ranchers
i have a little question-
if i synchronize static block on instance it runs fine, but is that different from getting lock of "Class" object as static methods holds by default for eg:
class a{
static void o() {

synchronized(new a()){ // 1
}

}

}
i mean to say am i getting lock on class represented by newly created instance at line 1.

Thanks in advance!
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i mean to say am i getting lock on class represented by newly created instance at line 1.


You are synchronizing with the instance of class A that you created at line #1. And since, it is impossible for any other thread to get access to that lock -- you don't save a reference to the object anywhere -- it pretty much doesn't do anything useful.

Henry
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To give a further example of locking with the "Class" object, and synchronization of static methods... All three of these methods are synchronizing on the same lock...



Henry
 
Atul Shukla
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!