File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread: static synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread: static synchronization" Watch "Thread: static synchronization" New topic
Author

Thread: static synchronization

shashank dwivedi
Ranch Hand

Joined: Mar 06, 2012
Posts: 61
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

Joined: Apr 19, 2013
Posts: 199
    
    1
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

Joined: Mar 06, 2012
Posts: 61
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

Joined: Jul 29, 2012
Posts: 734




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

Joined: Mar 06, 2012
Posts: 61
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread: static synchronization