Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

notify() question..

 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Waclaw Borowiec
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say - NO.
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then how come "C" is including into answers anyone can confirm??
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no end method, but notify doesn't stop/pause the current thread.
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21197
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21197
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I hope there are no such "debatable" questions on the real exam.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
me too
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic