• Post Reply Bookmark Topic Watch Topic
  • New Topic

calling two synchronised methods concurrently  RSS feed

 
marlajee Borstone
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class (say class 'A' ) which has two methods [ m1() and m2() ] both are synchronised.
Is it possible to call m1() and m2() of the same object of class 'A', by two separate threadts 't1' and 't2' concurrently:
t1.m1()
t2.m2()

?

What will happen if m1() is synchronised and m() unsynchronised ? will again it will be possible to call t1.m1() and t2.m2() simulteneously ?

appreciate the reply

Dhansumaal
 
Vijitha Kumara
Bartender
Posts: 3973
24
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marlajee Borstone wrote:I have a class (say class 'A' ) which has two methods [ m1() and m2() ] both are synchronised. Is it possible to call m1() and m2() of the same object of class 'A', by two separate threadts 't1' and 't2' concurrently:

Yes unless they synchronize on the same object (that is both methods are either instance methods or class methods hence they synchronize on same object, current instance or Class object for class A respectively).
What will happen if m1() is synchronised and m() unsynchronised ? will again it will be possible to call t1.m1() and t2.m2() simulteneously ?

Yes. No problem (but you should pass in a Runnable to the threads in order to run them, not like t1.m1())
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class (say class 'A' ) which has two methods [ m1() and m2() ] both are synchronised.
Is it possible to call m1() and m2() of the same object of class 'A', by two separate threadts 't1' and 't2' concurrently:
t1.m1()
t2.m2()


What do you mean by t1.m1() and t2.m2()?They don' seem to make sense. You are probably referring to a.m1() and a.m2(),where a is an object of class A,each being called from a separate thread (a.m1() in T1,and a.m2() in T2).

If above is the situation,only one of the threads T1 and T2 could be executing at a given point in time,simply because each of them would have to acquire a lock on a's monitor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!