File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread q from mughal

 
mike hanser
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that was weird Anyway, maybe we could test this with code...
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic