• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Thread: static synchronization

 
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 2447
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic