File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes notify() question.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "notify() question.." Watch "notify() question.." New topic
Author

notify() question..

Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

Which of the following may pause/stop the current thread?
Select 3 correct options

a calling Thread.yield()

b calling someObject.wait()

c calling someObj.notify()

d calling end() method on the Thread object.


the answers are a, b and c!
does notify() pause/stop a thread??

ref: jqplus


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Waclaw Borowiec
Greenhorn

Joined: Dec 14, 2009
Posts: 21
I'd say - NO.
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

then how come "C" is including into answers anyone can confirm??
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
there is no end method, but notify doesn't stop/pause the current thread.


SCJP 1.6 96%
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

So, Finally what i decide is: answers mentioned into jqplus are wrong for this question! or something else!! if any jqplus guy can explain?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

Minhaj kaimkhani wrote:does notify() pause/stop a thread??


The only way that I can see this happening is... the notification is sent to a higher priority thread, which in turn, causes a preemptive context switch. But even then, this can't happen until the current thread actually releases the lock.

So... yea. Choice C is wrong.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Henry Wong wrote:
The only way that I can see this happening is... the notification is sent to a higher priority thread, which in turn, causes a preemptive context switch. But even then, this can't happen until the current thread actually releases the lock.

So... yea. Choice C is wrong.

Henry


well Henry thats a good point which reminds of something I read in K&B that:
If a thread enters the runnable state, and it has a higher priority than any of the threads in the pool and a higher priority than the currently running thread, the lower-priority running thread usually will be bumped back to runnable and the highest-priority thread will be chosen to run.


so if notify() notifies a thread which has a higher priority than currently running thread then, it may cause the current thread to pause/stop.

which concludes that 'c' can be a correct answer.

am I right?
Waclaw Borowiec
Greenhorn

Joined: Dec 14, 2009
Posts: 21
Context may be switched to another thread at any time, notify() method isn't privileged here in any way. Answer C makes sense only if there's another thread waiting on the lock which notify() is invoked within. But even in this situation when the other thread will be notified it won't get into runnable state until the lock is released, which usually happens just AFTER notify() invocation. So for me C is a wrong answer.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

As I mentioned, this last issue is subtle enough to argue in either direction. You can say that the triggering of a context switch *is* causing the current thread to pause... but at the same time, it also needs a higher priority thread to be waiting, and the current thread to also release the lock -- so you can say that it is *not* enough to be causing the current thread to pause.

Either way, it is debatable. As for how I think about it, I think choice C is wrong. Why? A test question should target a single (and / or common) concept. This is neither a single or common concept.

Henry
Waclaw Borowiec
Greenhorn

Joined: Dec 14, 2009
Posts: 21
Yeah, I hope there are no such "debatable" questions on the real exam.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
me too
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: notify() question..