Forums Register Login

Java Threading

+Pie Number of slices to send: Send
Hi Guys.

I am preparing for OCJP/SCJP 6. I have came across below thread question but i didn't understand the answer of that question.
Please find below code , options and answer for the same. Please help me to understand the output of the code fragment.




Queation : Which of the following pairs of method invocations could NEVER be executing at the same time? (Choose all that apply.)


Options

A. x.a() in thread1, and x.a() in thread2
B. x.a() in thread1, and x.b() in thread2
C. x.a() in thread1, and y.a() in thread2
D. x.a() in thread1, and y.b() in thread2
E. x.b() in thread1, and x.a() in thread2
F. x.b() in thread1, and x.b() in thread2
G. x.b() in thread1, and y.a() in thread2
H. x.b() in thread1, and y.b() in thread2


Answers : A,F and H
+Pie Number of slices to send: Send
Hello Nitin,

Option A is true because every object comes with a single lock which a thread can acquire during synchronization so when thread1 has acquired x's object lock then thread2 will not able to get the same lock until it is release by thread1 and vice-versa.

Option F is correct because a static method belongs to a class so unlike instance method static method comes with only one lock which belongs to whole class and all the objects of the class share it, hence if thread1 gets this lock first then no other thread will able to get into static method b() until the lock is release and vice-versa

Option H is true because as I said static method doesn't belongs to any object so even though two different objects (x and y) are calling this method only lock is available which the threads needs to acquire so as to get into the method.


Bottom-line is if threads get separate locks on a synchronized method then they can access that method simultaneously for e.g option C where thread1 gets lock of x object and thread2 gets lock of y object as they have different locks they don't need to wait for each other for lock release in order to access the synchronize method a()

Thanks..
+Pie Number of slices to send: Send
 

Khuzema Dharwala wrote:static method belongs to a class so unlike instance method static method comes with only one lock which belongs to whole class and all the objects of the class share it, hence if thread1 gets this lock first then no other thread will able to get into static method b() until the lock is release and vice-versa



It's worth pointing out that the lock obtained by syncing a static method is not special. It's no different from the lock obtained by syincing a non-static method. All sync blocks and methods do exactly the same thing. They obtain an object's lock. Sometimes we explicitly state which object's lock we want to obtain, with synchronized (foo), and sometimes that lock is implicit, when we use the syntactic sugar of declaring a method synchronized. But even when we do that, it's still the same thing--we obtain an object's lock, and no other thread can obtain that lock until we release it. The fact that a synchronized static method happens to obtain the lock for an instance of java.lang.Class does not make it any different.
Everybody! Do the Funky Monkey! Like this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 763 times.
Similar Threads
Thread's tangle
chapter 9 self test Q.15
help me with Thread Problem
concurrent acess
Doubt with thread
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:28:18.