• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can two threads call two different synchronized instance methods of an Object?  RSS feed

 
Priyanka Dandekar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can two threads call two different synchronized instance methods of an Object?

I am still not sure on this, If I have two different methods in a object and two different threads are calling them.....is it possible?

Not sure if I understood the question right.... but looks like really fundamental question. Please provide your suggestions and pointers...
 
Sachin Joshi
Ranch Hand
Posts: 83
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. If a object has synchronized instance methods then the Object itself is used a lock object for controlling the synchronization. Therefore all other instance methods need to wait until previous method call is completed.
See Sample Code which demonstrate it very clearly.
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot call two synchronized methods of a class on the same object. This is the use of synchronization. If you can call two synchronized methods on the same object then there is a chance of corruption of the state of the object. Synchronization helps you to avoid this....
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you can call them, but one thread will block until the method of the other has finished, as SachinJoshi has said.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"SachinJoshi",

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
Maurizio Nagni
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yes you can... but may be is a bit different from what you expect.

As you can see the lock is based on two different objects, so two different threads can call them indipendently. Anway is up to you verify that they do not destroy the class integrity, that is you do not write some class property while the other method is trying to read it, but as far concern method internal properties you are free.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maurizio, the question was about synchronised methods i.e. something like
Here, if you had two threads referencing the same instance of DoubleCall and one of the threads called method1, then the second thread would be blocked from calling either method1 or method2 until the first thread returned from method1.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!