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 when will a thread stop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "when will a thread stop" Watch "when will a thread stop" New topic
Author

when will a thread stop

mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
I am posting this again
2. Under which circumstances will a thread stop
A. the method waitforId() in class mediaTracker is called
B. the run() method that the thread is executing ends
C. The call to the start() method of the Thread object returns
D. The suspend() method is called on the Thread object
E. The wait method is called on the Thread object
My understanding
A) I tried to track down the waitForId() method, which calls some other methods, which call some method of the toolkit class,
which are supposed to load the image. So untill the Image is loded the thread waits. If the thread is waiting, then it is not running so it has stopped (running).True.
B)The run method execution ends. The thread stops executing because there is nothing to execute, so it stops ( running).True
C) The start() method doesnt wait for the run method of the new thread to finish execution. start() method returns even if the thread of execution started by it is still executing.False.
D)Suspend Method is called on the new Object.(Do we need to know this for the exam?).By definition suspend Method will stop the thread untill it is resumed. True.
E)Wait method will also stop the thread from running, Untill it is notified by some other thread.True
My choices are not accepted by the Mughal Test.(It says only b is correct).
I am sure this question has been discussed before, but I still havent come across a definitive answer. Kindly tell me link to some previous discussion of this question.

[This message has been edited by mohit joshi (edited October 06, 2000).]
[This message has been edited by mohit joshi (edited October 07, 2000).]
Sathvathsan Sampath
Ranch Hand

Joined: Oct 03, 2000
Posts: 96
why don't you search for this topic in this forum.
There has already been lots of discussion on this topic. PLease go thro it u'll find it very helpful.
Also, search for user & daemon threads. These topics are also covered in the exam.

------------------

- Sathvathsan Sampath


- Sathvathsan Sampath
Jon Aryan
Greenhorn

Joined: Oct 06, 2000
Posts: 22
Its nice to understand the various "phases" a thread can have.
Like "running/runnable", "blocked", "dead".
Methods like waitforId(), wait(), sleep(), suspend() and the i/o methods actually "blocks" the thread from running, ie. its waiting for some events to happen, so that it can resume its run.The scheduler won't consider the "blocked" thread, and its not occupying the CPU, but the thread is alive.
A thread successfully finishes execution when it returns from the run() method, and the thread is considered as "dead".
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Hi everybody!
I put this small table to be discussed and corrected:
(later inserts: I do not know why there are so much blank space before table, please, scroll the page down)
What may/can/will stop a running thread?

running -> blocked

running -> dead

may

will

may

will

"blocking"

wait()

�+

+

-�

-

yield()

�+ (running -> ready)

+ (running -> ready)

�-

-

sleep()

�+

+

�-

-

suspend() (DEPRECATED)

�+

+

-

-

the method waitforId() in class mediaTracker is called

�+

+

-

-

A call to the halt or pause method of the Thread class

no such methods exist

I/o methods (a read() call on an InputStream)

�+

-

-

-

attempt to obtain a lock (to enter synchronized code block)

+�

-

-

-

exits

When the main method completes (assuming that question asks about another thread, not main thread )

-�

-

-

-

The call to the start() method of the Thread object returns

-�

-

-

-

the run() method that the thread is executing ends

�-

-

+

+

The program exits via a call to System.exit()

�-

-

+

+

another thread

If a thread creates a new thread

�+ (eventually)

-

-

-

Another thread is given a higher priority

�+

-

-

-

There is an uncaught exception in another thread

�-

-

-

-

stops

A call to the thread's stop() method.(DEPRECATED)

�-

-

+

+

A call to the thread's destroy() method.

�This method is not implemented

interrupting

Interrupt() is invoked

-

-

+

-

When the thread throws an interrupted exception

-

-

�may or may not

�-


Warning: do not use this table as reference, it may have mistakes! Please, post your comments.
[This message has been edited by Mapraputa Is (edited October 06, 2000).]


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243

I think what the Question's author meant was : when will the thread be dead. He felt free to use the term stop, because the deprecated method stop throws a ThreadDeath exception resulting in thread death.
How ever the word "stop" is used in many different contexts ( in sun's documentation also ).Here is an part of text that explains why stop was deprecated:
"What should I use instead of Thread.stop?
Most uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running....".
so in present terminology stop is not equivalent to a thread dying.It could also mean "stop running".
This is my interpretation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when will a thread stop