• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Threading  RSS feed

 
Nitin Piparava
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Khuzema Dharwala
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!