Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static and non-static synchronized method

 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anil Gautam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Monu, its really a good link about thread synchronization.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic