• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread synchronization

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post your code (usimg code tags)
 
Satyajeet Kadam
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have code.
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your reply
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ... ...
reply
    Bookmark Topic Watch Topic
  • New Topic