• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread: static synchronization

 
shashank dwivedi
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey I want to clear my doubt on static synchronization. suppose i have a class like



now suppose we have two objects of this class as object1 and object2 and two threads t1 and t2.
now if t1 is accessing static method1() on object1 which methods can then be accessible simultaneously by t2 ??
I think non-static method of any object can be accessed by t2 but i am not sure.
 
Carles Gasques
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I understand that since the only method locked is method1 by thread1 the other methods from A could be accessible.

Since method1 is static is locked for obj1 and obj2.


Best regards,
 
shashank dwivedi
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carles Gasques wrote:Hi,

I understand that since the only method locked is method1 by thread1 the other methods from A could be accessible.

Since method1 is static is locked for obj1 and obj2.


Best regards,



Thanks Carles

 
Himai Minh
Ranch Hand
Posts: 1256
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Case 1 :When two threads running method1 and method2, these two threads need to be synchronized on the same A.class object.
Case 2: When two threads running method1 and method3, they don't need to be synchronized with each other as A.class object and an instance of A is not the same object.
Case 3: When two threads running method1 , they need to be synchronized on the same instance of A.class object. This is the same as case 1.
Case 4: When two threads running method3, they need to be synchronized on the same instance of A.

I hope these 4 cases can help.

 
shashank dwivedi
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:



Case 1 :When two threads running method1 and method2, these two threads need to be synchronized on the same A.class object.
Case 2: When two threads running method1 and method3, they don't need to be synchronized with each other as A.class object and an instance of A is not the same object.
Case 3: When two threads running method1 , they need to be synchronized on the same instance of A.class object. This is the same as case 1.
Case 4: When two threads running method3, they need to be synchronized on the same instance of A.

I hope these 4 cases can help.



Shashank Dwivedi wrote: Thanks Himai
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic