• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ....https://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 ]
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ....https://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
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Hug your destiny! And hug this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic