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 ///thread/// Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "///thread///" Watch "///thread///" New topic
Author

///thread///

chao-long liao
Ranch Hand

Joined: Jul 29, 2001
Posts: 78
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

Joined: Jan 08, 2002
Posts: 411
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

Joined: Jul 03, 2001
Posts: 2120
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".


SCJP2. Please Indent your code using UBB Code
Blanka Li
Greenhorn

Joined: Feb 18, 2002
Posts: 13
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

Joined: Aug 29, 2001
Posts: 219
So what are the answers?


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
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

Joined: Jan 08, 2002
Posts: 411
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

Joined: Jun 04, 2002
Posts: 133
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ///thread///
 
Similar Threads
Marcus Green Exam 2 Question 22 On threads
Marcus Green exam 2
Threads Question.
Marcus Mock exam 2 Threads
setPriority()?