aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread synchronization" Watch "Thread synchronization" New topic
Author

Thread synchronization

Satyajeet Kadam
Ranch Hand

Joined: Oct 19, 2006
Posts: 215
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

Joined: Jun 01, 2010
Posts: 618
Please post your code (usimg code tags)
Satyajeet Kadam
Ranch Hand

Joined: Oct 19, 2006
Posts: 215
I don't have code.
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 784

@ 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


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Satyajeet Kadam
Ranch Hand

Joined: Oct 19, 2006
Posts: 215
thanks for your reply
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread synchronization
 
Similar Threads
synchronized keyword use
Synchronizing Static Methods
need synchronize clarification
Reg : Synchronization access
Accessing Synchronized methods in a single object