• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

SCJP: Threads Qs

 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A)
if we have 3 synchronized methods inside same obj, then must be executed (by another obj) in case, all synchronized methods in queue, after another obj execute the synchronized methods again all (finished), after other, and so on...? queue of synchronized methods get executed by one after the other obj, required be the same?

B)
in a class obj we have 3 synchronized methods and 3 UN-synchronized methods, well, when an obj-A call 1st synchronized method and finish, may an obj-B call 1st synchronized method and finish, AFTER obj-A call 2nd synchronized method all sychr methods same obj and so on... ?

C)
I]A thread may acquire 2 or more Locks of synchronized methods/blocks. TRUE/FALSE
II]When we have Two or more Static synchronized methods same class, this is correct (below)?
MyClass.class
.....
public static synchronized int getCount() {
return count;
}
public static synchronized int getCount2() {
return count;
}
.....

OR

MyClass.class
.....
public static int getCount() {
synchronized(MyClass.class) {
return count;
}
}
public static int getCount2() {
synchronized(MyClass.class) {
return count;
}
}
.....
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A thread may acquire 2 or more Locks of synchronized methods/blocks. TRUE/FALSE



This is not a proper question.

Locks are for objects and NOT FOR methods

If you are just talking about a thread acquiring one or more locks on different or distinct objects then it's TRUE.
If your question is , "A thread may acquire 2 locks on the same object" then it's FALSE . (AN OBJECT HAS ONLY ONE LOCK)


*********************************************


MyClass.class
.....
public static synchronized int getCount() {
return count;
}
public static synchronized int getCount2() {
return count;
}
.....

OR

MyClass.class
.....
public static int getCount() {
synchronized(MyClass.class) {
return count;
}
}
public static int getCount2() {
synchronized(MyClass.class) {
return count;
}
}





This will work fine . These are just two equivalent representations.


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic