• 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

Marcus Exam#2 Q42

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<code>
Which of the following statements about threading are true
1) You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable
2) You can obtain a mutually exclusive lock on any object
3) A thread can obtain a mutually exclusive lock on a method declared with the keyword synchronized
4) Thread scheduling algorithms are platform dependent
</code>
The answer given are 2,3 and 4.
My first doubt: Answer 2) A thread can obtain a mutually exclusive lock on any object.
I think it can obtain the lock only on those objects whose class contains the synchronized method.
My second doubt: Answer 3) can a thread obtain a mutually exclusive lock on a method OR should it be object? If so then should the answer be reworded as " A thread can obtain a mutually exclusive lock on an object when the object invokes a method which has the keyword synchronized".
I have read the JLS on this topic and haven't really got any convincing answer.Does anyone recommend a better source to study threads?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
For Option 2) the locks are associated with objects and not with methods. So even if an object doesnt have any method synch/unsynch, its possible to lock it.
e.g.
class A {
int[] a;
void aMethod() {
synchronized(a) {
//..do some processing here.
}
}
}
Option 3) Your interpretation is right.
U can refer to "Java Threads" by O'Rielly Pub.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howard,
Regarging lock of an object
1.Each object has a mutually exclusive lock.<pre>
synchronized(objectRef) {
}
</pre>
2.lock is associated with objects NOT methods.
3.when you put 'synchronized' keyword before a instance method you are obtaining the lock of 'this' object which is the current object. Eventhough it looks like the METHOD is synchronized.<pre>
synchronized void m1() {
}
</pre>
4.every object has a Class object associated with it.
when you put synchronized keyword on a class method what it is doing is , the lock associated with the 'Class' object is obtained. (i.e)
<pre>
static synchronized void m1() {
}

is equivalent to
void m1() {
synchronized(Class.forName("classNameOftheCurrentObject")) {
}
}
</pre>

So ans 2) is correct.
ans 3) is poorly worded. Your analysis seems to be ok.
regds
maha anna
[This message has been edited by maha anna (edited April 01, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Maha and "JFAM". When I posted the question I think I forgot that there are two things
a) synchronized statements and
b) synchronized methods.
A thread can acquire a lock on any object through a synchronized statement as shown in Maha's example in point 1).
However a thread CANNOT acquire a lock on any object through a synchronized method it can only obtain a lock on the this object i.e the object of the class in which the method is defined or any object that is an instance of the subclass of the class in which the method is defined. If the class in question is static then the lock associated with the 'Class' object is obtained.
Thought I would add this just to be more complete.

[This message has been edited by Howard Stern (edited April 02, 2000).]
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic