Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Q from Whizlabs

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Mock Exam practice number 9 from Whizlabs 4.0, Question number 27 says:
"Which of the following statements are true?
a) A Thread's start() method, automatically call the run() method
b)...
c)...
d)...
"
It says option a is true, But I think it is not, because start() method does not call run() method, It just put the receiver thread in a runnable state, and then, The Thread scheduler will decide when to invoke it's run() method...(maybe never!)
Am I rigth?
Thanks,
Claudio
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Claudio,
I think your understand is right,only the scheduler can decide which thread to run.
but this answer doesnt care whether the run() MUST be run,so this issue has no problem
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Claudio, I agree with you.
The Java Programming Language, section 10.1 Creating Threads says "The start method spawns a new thread of control based on the data in the Thread object, then returns. Now the virtual machine invokes the new thread's run method, making the thread active."
I wonder if what Whizlabs was trying to say is that the client calls start() but does not call run().
[ November 28, 2003: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Marlene Miller:
Hi Claudio, I agree with you.
I wonder if what Whizlabs was trying to say is that the client calls start() but does not call run().
[ November 28, 2003: Message edited by: Marlene Miller ]


Hello Marlene
Now 'am in a fix what r we supposed to mark in the exam. I always thought that that the start() calls the run(). Coz i had read somewhere that we do not directly call the run().
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mona,

Now 'am in a fix what r we supposed to mark in the exam.


On the exam, I would say start() does not call run().

I always thought that that the start() calls the run(). Coz i had read somewhere that we do not directly call the run().


We do not know for sure what happens inside start(), because we do not have the source code. start() is a native method.
public synchronized native void start();
----
I assume start() does not call run() for the following reason. Suppose the main() method calls start(). That is, the main thread executes start(). If start() called run(), the main thread would execute run(). But we know for sure the main thread does not execute run(). Instead the new thread executes run().
Two threads execute two different paths through the code. One thread executes start(), the other thread executes run(). Sometimes the virtual machine executes one path, sometimes the other path.
----
I believe the book The Java Programming Language is trustworthy. One of the coauthors is James Gosling, the creator of the Java language.
"The start method spawns a new thread of control based on the data in the Thread object, then returns. Now the virtual machine invokes the new thread's run method, making the thread active."
----
We can almost prove start() does not call run().

The first time, it looks like start() returns and then the virtual machine executes run(). The second time, I increased the priority of the new thread. It looks like the virtual machine executes run() and then start() returns.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ November 30, 2003: Message edited by: Marlene Miller ]
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic