• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exam cram

 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have an application that executes the following line: Thread myT = new Thread(); Select all of the following statements that are correct. [Check all correct anwers]
A) The Thread myT is now in a runnable state.
B) The Thread myT has the priority of the Thread that executed the construction statement.
C) If myT.start() is called, the run method in the class where the construction statement appears will be executed.
D) If myT.stop() is called, the Thread can later be started with myT.start() and will execute the run method in the Thread class.

What is the answer for this question? Please explain in detail
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A. False - start hasn't been called, all you did was create a new thread.
B. True - Any thread you start will by default have the same priority of the thread that created it. You can change this, but you didn't in your code.
C. True - Since this wasn't created by passing in the class that implements runnable, it will use the run method of the class that has this statement.
D. False - Once a thread stops, it can't be started again.
Bill
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about answer c? Bill, you say it's true, but wouldn't calling the start method only make it eligible to run and it's up to the thread scheduler if it gets run or not. I agree more often than not, it would actually run, but isn't there a chance that it won't?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C is false - the construction statement was
Thread myT = new Thread();
which does not attach a Runnable object. Therefore when the run() in Thread is executed, it will be empty. Here is the code in Thread:
public void run() {
if (target != null) { target.run();
}
}
target is of course the Runnable reference, in this case null.
C. the run method in the class where the construction statement appears will be executed.

------------------
author of:
 
kapil apshankar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,
I am still not clear as to why option c is false. Because sooner or later will not the run() method execute? Although the start() method will not directly execute this method, it will nevertheless register the thread making it eligible to run at a later point.
Thanks and regards,
Kapil
 
debasish tripathy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread myT=new Thread();
If myT.start() is called, the run method(which is a do nothing method) of the Thread class will be executed not the run method of the class where the construction statement appears.
If it were
class MyThread extends Thread{
public void run(){
//does something }

public void someFunction(){
Thread myT=new MyThread();
myT.start();} }

then the run method of the class where the construction statement appears will be executed.Because in the above case the run() method of MyThread overrides the run() method of Thread.
Hope this helps
 
Wong KK
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question drops in what construction statement means?
Is construction statement means : new Thread()??
or means the constructor of Thread class??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic