• 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

thread question required

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why alternate E is correct .
please throw some light on this.

Mohit
Would be SCJP.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic