This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Thread synchronization  RSS feed

 
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?




 
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.
 
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
 
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 ... ...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!