wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread question: extending Thread class and start() 2 instances ? 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 question: extending Thread class and start() 2 instances ?" Watch "Thread question: extending Thread class and start() 2 instances ?" New topic
Author

Thread question: extending Thread class and start() 2 instances ?

david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
Here's some code taken from one of Marcus Green's practice exams ( http://www.jchq.net/mockexams/exam1.htm#Question 54 )- altered slightly to observe behaviour:

Question 1:The first time I ran this code, two, one, two, one was output.
On most sunsequent runs, the output was one, two, one, two.
Is it right to assume that it is NOT garunteed that the threads will perform in order that they are started? (this seems to be the case)
Question 2:Is it reasonable to assume, that if a question asks you about the output order, that something other than separate threads have been invoked? (which is the case with Marcus's question - his code called the defined run() as a normal method call within the "main" thread of execution)
Question 3:is my comment here correct?
pm1.start(); // call's the run() method of
// the Thread class, which calls
// the subclass run()
[ November 14, 2002: Message edited by: david eberhardt ]
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Q1) When 2 or more threads are called the order of execution is not neccessarily the order in which the threads are started.
Q2) When run is called it does not create a thread it just calls the instance method run..
of Q3 I am not sure of the answer...


Sunita<br />SCJP 1.4
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
About Q.3) pm1.start(); calls the overriding run() method in subclass.If you do not override the run() method in your class it call the run() method of thread class.
To learn more go through the same discussion had before ....http://www.coderanch.com/t/239859/java-programmer-SCJP/certification/Dan-thread-exam
Hope this helps you
regds
Arpana
[ November 14, 2002: Message edited by: Arpana Rai ]

SCJP1.4(91%)
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
Originally posted by Arpana Rai:
About Q.3) pm1.start(); calls the overriding run() method in subclass.If you do not override the run() method in your class it call the run() method of thread class.
To learn more go through the same discussion had before ....http://www.coderanch.com/t/239859/java-programmer-SCJP/certification/Dan-thread-exam
Hope this helps you
regds
Arpana
[ November 14, 2002: Message edited by: Arpana Rai ]

clarification -
to start any thread, we call start() method on the Thread instance:
ex. testThread.start();
From "Java2, Sun Certified Programmer for Java 2, Study Guide" by Syngress/Osborne I read the following:
In the case of a Runnable target, its run method is not called directly, but it is called by the default run method of the Thread instance."
"
I guess what I was trying to clarify is that the above comment applies to both subclasses of Thread class and also Objects that implement the Runnable interface.
-------------------------
let's say you have the class I mentioned in the above code. Instead of calling pm1.start(); we call pm1.run(); instead (as was the code when Marcus wrote the test question).
If we call pm1.run(); we will be running in the main thread and the method "run()" behaves like a normal instance method (inside main's thread of execution).
So if we call pm1.start(); instead, something must occur to create a separate thread of execution before the contents of run() method of Pmcraven gets called, and the run() method then executes within it's own thread.
-----------------------------------
Marcus Green's exam had several good questions which really forces you to learn the ins and outs of these start and run calls!
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
ok - think I figured out my question.
I create an instance of a class which "extends Thread" (i.e class Pmcraven extends Thread {)
I call Pmcraven pm1 = new Pmcraven();
then pm1.start();
the start method of the superclass "Thread" will be called since I did not override that method in Pmcraven and so the documentation says the following:

java.lang
Class Thread
public void start()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
The result is that two threads are running concurrently: the current thread (which returns from the call to the start
method) and the other thread (which executes its run method).


so let's go find the right run() method:
java.lang
Class Thread
public void run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is
called; otherwise, this method does nothing and returns.
Subclasses of Thread should override this method.

I think I understand it now.
[ November 14, 2002: Message edited by: david eberhardt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread question: extending Thread class and start() 2 instances ?