• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread synchronization

 
Satyajeet Kadam
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is Class A in that i am having 2 synchronized methods, and 2 differnt objects(locks) to access each of these methods(i.e.lock=object that is used to access the synchronozed method).

Q1) Can two different threads can access 2 different synchronized methods of the same class using different locks ?

if thread1 access lock on synchronized method1, other thread cannot use the same lock to access to same synchronized method1.It has to wait in the object poll until the lock get available.
Now my question is

Q2) Can thread2 access same synchronized method1 using different lock at that time?




 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your code (usimg code tags)
 
Satyajeet Kadam
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have code.
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ amolpalekar
Whatever I understand from your statement I created following code
See code below



Output of the program

Main Done
thread1 before lock1.method1
thread1 calling method1
thread1 after lock1.method1
thread1 before lock1.method2
thread1 calling method2
thread1 after lock1.method2
thread1 before lock2.method1
thread1 calling method1
thread2 before lock1.method1
thread2 calling method1
thread2 after lock1.method1
thread2 before lock1.method2
thread2 calling method2
thread2 after lock1.method2
thread2 before lock2.method1
thread1 after lock2.method1
thread1 before lock2.method2
thread1 calling method2
thread1 after lock2.method2
thread2 calling method1
thread2 after lock2.method1
thread2 before lock2.method2
thread2 calling method2
thread2 after lock2.method2


If thread1 have lock1 then thread2 can't get lock1 and if thread1 have lock2 then thread2 can't get lock2 at same time
If thread1 have lock1 then thread2 can have lock2 and if thread1 have lock2 then thread2 can have lock1 at same time

Q1) Can two different threads can access 2 different synchronized methods of the same class using different locks ?

It is possible
Q2) Can thread2 access same synchronized method1 using different lock at that time?

It is possible but thread1 should not have any of two locks or thread1 should completed method call and release both locks.

I hope this may help you
Correct me if I am wrong
 
Satyajeet Kadam
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A synchronized method can be freely shared. It is an object's lock
that cannot be shared. So many threads can be stepping through
the same method, swapped in and out by the JVM, as long as each
holds a lock on a different instance.

Jim ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic