• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads Execution

 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which methods may cause a thread to stop executing?
sleep();
stop();
yield();
wait();
notify();
notifyAll()
synchronized()
I feel the answer is 1,2,4
But given answer is 1,2,3,4
I think yield will stop the thread only if the other high priority thread or equal priority thread is waiting. If no threads are in waiting the yield method executes the current thread.
But look the other question
which of these will always cause a running thread to temporarily stop?
A)Calling a sleep()
B)calling yielsd()
C)The run() calling system.exit(0)
D)starting another thread with a higher priority thread
E)None of the above
Answere is a
I do agree with this
Any other answrs
Thanks in advance
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may cause vs. will always cause
 
Stephen Tallamy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is that the first question says may cause a thread to stop executing. As you rightly say, calling yield may stop the thread if another thread is scheduled to start (this depends on the scheduler).
In the second question it says, will always cause a running thread to temporarily stop. sleep() is the only thing listed that does this.
It's all in the wording of the question
 
doug thompson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what happens when sleep( 0 ) is called?
 
Deepti Dixit
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the question :
Which of these will always cause a running thread to temporarily stop?
A)Calling a sleep()
B)calling yielsd()
C)The run() calling system.exit(0)
D)starting another thread with a higher priority thread
E)None of the above
Why is that only A is the correct answer. D would also cause the current thread to stop temporarily. Is it not so ?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A synchronized method or a synchronized block *may* stop a thread from executing if another thread already has the monitor for the object in question.
Is the reason that item #7 of the first question (synchronized()) not a valid answer because the question asked specifically about which "methods" may cause a thread to stop, and synchronized() is not really a method?
Seems like a subtle play of words.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I answered my own question 3 minutes later.
"synchronized" is a modifier, not a method.
silly me
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic