aspose file tools*
The moose likes Threads and Synchronization and the fly likes Static and non-static synchronized method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Static and non-static synchronized method" Watch "Static and non-static synchronized method" New topic
Author

Static and non-static synchronized method

Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Hey All,

Please help in knowing this -
1. A class having static and non-static synchornized method


There are two threads that are trying to call t1.m1 and t2.m2()... is it possible to call both the method. As per my understanding it is correct as one is class level method. Please give your inputs.

2. Is it possible to call t1.m1() and t2.m2(). Here i think its not. Please correct me if i am wrong.

And apologize if i am not clear enough to make clear my question.

Regards
Patricia Samuel.
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Any method can certainly be invoked. But according to my understanding if a method is declared as static then the Thread trying to invoke will need to obtain a lock on the Object with which the method is being invoked. In case of a Static method since there need not be an Object for invoking the lock ought to be on the Class.

Does that answer your query?

Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Hey Raj,

Hey I am really novice to thread and it might be a lame question .

But help me to get it - if there are two methods getObject() and setObject() that are set synchronized and if two threads can simultaneously access these method then what purpose it solves to make them synchronized.

Thanks a lot.

Regards,
Patricia.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Patricia Samuel wrote:... if there are two methods getObject() and setObject() that are set synchronized and if two threads can simultaneously access these method then what purpose it solves to make them synchronized.

It depends on the kind of synchronization used. If both are synchronized instance methods and both threads are try to invoke two methods on the same instance of the object then only one thread is allowed to execute.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Patricia Samuel wrote:There are two threads that are trying to call t1.m1 and t2.m2()... is it possible to call both the method. As per my understanding it is correct as one is class level method. Please give your inputs.

Yes, I believe the lock on the class and the lock on the instance are different.

Patricia Samuel wrote:
Is it possible to call t1.m1() and t2.m2(). Here i think its not. Please correct me if i am wrong.
.

No, once a thread obtains a lock on the instance only that thread can access ANY synchronized method of that instance.

Try this code:


The output of the above program illustrates these points.

HTH,
Nidhi


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Hey Nidhi,

Thanks for devoting the time. I did run this program and my analysis is as follow -

1)At any instance of time we can have m1 and (m2 or m3) method as an output.
2) But we can not have m2 and m3 both eb- m2 Red15 m3 Blue1 m2 Red16 m3 Blue2 . Reason being thread of same object can not access two non-static synchronized method until the first one gets finished.

Please correct me if i have missed anything in understanding.

And thanks again.

Regards,
Patricia

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Patricia Samuel wrote:
1)At any instance of time we can have m1 and (m2 or m3) method as an output.
2) But we can not have m2 and m3 both eb- m2 Red15 m3 Blue1 m2 Red16 m3 Blue2 . Reason being thread of same object can not access two non-static synchronized method until the first one gets finished.

That's exactly my analysis of the output too. And it matches plum with the way things are supposed to work regarding locks & synchronized static, non-static blocks.
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Yes, I believe the lock on the class and the lock on the instance are different.

As an aside, if you want to know more about Object locking, Monitors and Thread Synchronization (from JVM's perspective) you can read this


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Anil Gautam
Greenhorn

Joined: Apr 15, 2008
Posts: 6
Thanks Monu, its really a good link about thread synchronization.
 
 
subject: Static and non-static synchronized method