Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread question required

 
Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I interpret "Exiting from a synchronized block." to mean that the thread exits from a block of code which has the lock on an object- thereby allowing other threads to access this objects lock (and execute synchronized blocks on it as well)

I don't interpret it to mean the program ends. Nor does it mean that the currently executing thread will necessarily suspend- why would it? What if you had a single thread application with lots of synchronized blocks in it- would the thread cease (for no other thread) simply because it had left a synchronized block?

A synchronized block does not imply anything about the thread being run. A thread can be put into a wait state even if it is holding object locks.
 
Inuka Vincit
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom yeah I was about to correct him. You question is clear.

a thread leaving the syncronized block mearly means that the thread gave up the lock and not that its going to stop running. Only the VM will decide that.

check this tutorial out it explains things pretty clearly
http://java.sun.com/docs/books/tutorial/essential/threads/index.html
 
PETER CARTER
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which two can directly cause a thread to stop executing? (Choose Two)
A. Exiting from a synchronized block.
B. Calling the wait method on an object.
C. Calling the notify method on an object.
D. Calling the notifyAll method on an object.
E. Calling the setPriority method on a thread object.
Answer: B, E
Why A is wrong ??
Thanks!!!
 
Colin Fletcher
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to look at it is:

The exit directive indicates the program/application should stop running. The application will exit, why would it matter if you "close the resource" in the finally block?
 
Mohit Agarwal
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why alternate E is correct .
please throw some light on this.

Mohit
Would be SCJP.
 
Louie van Bommel
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mohit Agarwal:
Why alternate E is correct .
please throw some light on this.

Which two can directly cause a thread to stop executing? (Choose Two)
A. Exiting from a synchronized block.
B. Calling the wait method on an object.
C. Calling the notify method on an object.
D. Calling the notifyAll method on an object.
E. Calling the setPriority method on a thread object.
Answer: B, E


The thread scheduler will frequently (but certainly not always) schedul OUT a thread if a thread of higher priority is available (runnable) and make the HIGHER priority thread execute. (running)

If the thread which currently has the processor is changed to a lower priority, a higher priority thread may be scheduled to execute by the thread scheduler.

It's debatable if this can be viewed as "directly cause a thread to stop executing" because it's the THREAD SCHEDULER that's directly causing the thread to stop executing. (Unlike the test, I'm making assumptions about the test-writer's INTENTION vs. the wording).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic