Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EXAMCRAM CD Q:TEST 2, Q 11

 
ZEESHAN AZIZ
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


QUESTION:
You have an application, which executes the following line.
Thread myT = new Thread();
myT.start();
Sellect all answers:
a: Thread myT is now in a runnable state.
b: Thread myT has NORM_PRIORITY
C: Thread will die without accomplishing anything.
d. run method in class where statement occurs will be executed.
According to tester, answers are a, c.
However I think it is not correct. I think when new instance of Thread is created, it will be in ready state, which makes choice one wrong.
I think newly created thread instance will have normal priority attached to it, which make 2 a right choic, in my consideration.
I think c is not right, while d is correct.
I would like to confirm my assumptions.
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b might be correct. However, new threads are created with the same priority as the current thread, so if we've changed priority before the above statments, it will not have NORM_PRIORITY.
d on the other hand is not correct. Since you are creating an instance of Thread, not the class you're in, Thread's run()-method (which does nothing, so c is correct) will be executed.
Does this explain it?
/Mike
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know, I put my email address in that book for a reason - so readers could ask me questions like this.
wbrogden@bga.com
Mikael's answer is correct.
Bill
 
Rashmi Hosalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
So,is 'b' the right answer too or is it only 'c'?
Rashmi
 
akila raman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The start() method puts the thread in the ready to run state. So
option a is not valid.Given the following lines of code we are not changing the priority of thread so it has NORM_PRIORITY and option b is valid and option c is valid too and d is not correct.
- akila
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

b: Thread myT has NORM_PRIORITY -- WRONG
A new Thread gets the priority of the Thread that created it as Mikael already said.
a. Thread myT is now in a runnable state. -- TRUE
My copy of Java Threads says "A thread is in the runnable state once its start method has been called."
Bill

------------------
author of:
 
Annie Naqvi
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Sorry Bill I do not get your point... as I read in Simon Robert's guide that when start() method is called it makes the thread "eligible to run" and that the thread is still contend for CPU time.Thus it is in the "ready" state and not in "runnable" state.
And that's what I know.
Your comments required Bill.
Regards,
Annie
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think the difference between what you are calling the ready state and what I am calling the runnable state is?
I don't see any difference. If a Thread is runnable, it may be scheduled for running time by the JVM at any time.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic