It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread q from mughal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread q from mughal" Watch "thread q from mughal" New topic
Author

thread q from mughal

mike hanser
Greenhorn

Joined: Aug 01, 2002
Posts: 8
hi,
can anyone shed light on a review question on threads from Mughal?
question:
==============================================
"Which of these are plausible reasons why a thread might be alive, but still not be running?"
Select all valid answers.
(a) The thread is waiting for some condition as a result of a call to wait().
(b) The thread is waiting on a monitor for an object so that it may access a certain member variable of that object.
(c) The thread is not the highest priority thread and is currently not granted CPU time.
(d) The thread is sleeping as a result of a call to the sleep() method.
==============================================
Ans: a,b,c,d
Regarding answer choice "b", it is my understanding that synchronized blocks prevent other threads from executing inside those same synchronized blocks.
Wouldn't this mean that other threads can access instance variables of an object while a thread is executing inside a synchronized block of said object?
If that is the case, wouldn that not weaken the claim made by choice "b", in other words, can it be rephrased as: "the thread does not neccessarily have to wait on a monitor for an object, it can simply access non-synchronized members at any time."
I appreciate any comments!
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
This seems be to Mughal 9.13 (p.293). The answer is: a, c, d.
b is excluded because: "Access to member variables is not governed by monitors, i.e. variables cannot be directly synchronized. Only methods and code blocks can be synchronized."
mike hanser
Greenhorn

Joined: Aug 01, 2002
Posts: 8
hi anthony, thanks for the response. are you using a newer edition of Mughal than I am? (b) is given as part of the answer on p.647. hmmm maybe there's an errata somewhere for this book, will try to look it up.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Wow, that was weird Anyway, maybe we could test this with code...
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Here is a sample that I made. Crude, but it'll have to do, I guess...

Thread A gets the monitor lock of a common Runner object, but Thread B has access nonetheless of the Runner object's instance members.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: thread q from mughal
 
Similar Threads
Thread question from mock
unlock method
Threads--JQ+
notify() which thread
Threads and Synchronization examples