• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

///thread///

 
chao-long liao
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JohnJuntMockExam...
Which methods may cause a thread to stop executing?
a.sleep();
b.stop();
c.yield();
d.wait();
e.synchronized()
my answer are acd,but the answer are ab.
method stop() is depreciated,so we can skip it.
my question is option c and d??
They can put the current executing thread into waiting state.It's their definition,isn't it??
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If you've more than two threads and you've called yield() on first thread, it's possible that this thread will stop running and let other threads to run.
But if you've just one running thread and you call yield() on this, it will not stop - because there's no another thread...
The same concerns wait().
Jamal Hasanov
www.j-think.com
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I disagree with the same concerns wait()
If the only thread is made to wait, it is still waiting. I mean it was clearly "stopped".
 
Blanka Li
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait() and notifyAll() are only used in a code paragraph signed by synchronized,no matter a thread exists or not. So in a thread, if there is no synchronized code, wait() can't be used. So the thread can't be stopped using wait method.
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what are the answers?
 
Alan Chong
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jamal,
Are you sure that if a program doesn't create
any other thread there will be just the main
thread ?
How about the garbage collector though of low
priority ?
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Alan
Nice question - I don't know what happens when GC waits...? but I'm sure that yield() doesnt' stop thread any time. It depends on situation.
P.S. Maybe GC is JVM's private thread - therefore main thread doesn't stop...
Additionally you can read JLS, CHAPTER 17 - Threads and Locks
Jamal Hasanov
www.j-think.com
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait() and notifyAll() are only used in a code paragraph signed by synchronized,no matter a thread exists or not. So in a thread, if there is no synchronized code, wait() can't be used. So the thread can't be stopped using wait method.

This makes no sense to me at all. Having the wait() method in synchronized code or not does not affect its ability to stop the thread. (Btw, An IllegalMonitorStateException runtime exception is thrown when wait() and notify() isn't in synchronized code.)
My answers are all of them, actually. When the thread encounters synchronized code and it doesn't have that object's lock, it stops executing and waits until it obtains that lock.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic