Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Savio Mascarenhas
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q3.[Check all correct answers]
You have an application which executes the following line:
Thread myT = new Thread();
myT.start();
Select all the following statements that are correct.
(a)The Thread myT is now in a runnable state.
(b)The Thread myT has NORM_PRIORITY priority.
(c)The Thread will die without accomplishing anything.
(d)The run() in the class where the statement occurs will be executed .
The answers mentioned were (a) & (c).Should (c) or (d) be selected . Should'nt (b) be included since,if not explicitly set a new thread has Normal priority.
 
Deepak Magoo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Savio
I think that the answers should be a,b,d. a is correct coz when u create an instance of thread it is in runnable state. anwer b is correct coz the default priority as u say for a thread is NORM_PRIORITY. i do not understand the third option. the last option should be correct because the start() is going to call the run(). tell me if i am correct or wrong.
Deepak
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The thread will have the same priority as the parent thread.

 
naveen sahu
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Savio Mascarenhas:
Should'nt (b) be included since,if not explicitly set a new thread has Normal priority.

no , b can't be 100% true always.
because if not explicitly mentioned a thread always have priority equal to that of creating thread. and creating thread may have a priority b/n 1 to 10.
 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Select all the following statements that are correct.

(a)The Thread myT is now in a runnable state.
(b)The Thread myT has NORM_PRIORITY priority.
(c)The Thread will die without accomplishing anything.
(d)The run() in the class where the statement occurs will be executed .

I think (A) is the only correct answer.
start method will make the thread runnable.... it is upto the JVM/OS to decide when the thread will run.(which explains (d) is False, bcoz start method does'nt call the run).
The newly created thread will always have the priority of its parent thread. so (B) is false.
Regarding (c).... It think it is False... but I still doubt....
can anyone throw some light on it....

Thanks
Aruna
[This message has been edited by Aru Ven (edited December 07, 2000).]
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
(a) True. Invoking start() places the thread in the Runnable state.
(b) False. Threads inherit their priority from the thread that starts them which may or may not be NORMAL_PRIORITY.
(c) True. They haven't given any code to implement run().
(d) False. Invoking start() allocates system resources, schedules the thread, and calls run() BUT when it actually executes depends on the Scheduler (See Sun Java Thread tutorial).
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just for clarity, the code given has constructed a thread object without passing any runnable, ( and has also not made object of a subclass of Thread to provide implementation of run). So the default run method doesnot do anything.
Doesnt thread priority vary from 0 to 10, that is 11 different levels of it?? I remember reading this somewhere.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C is correct because the the constructor of the Thread takes a Runnable object. Since none was passed, we are starting a default Thread which does nothing. In order for the run() method of the included class to be invoked, we must instantiate the Thread with:
Thread myThread = new Thread(this);
 
ming fan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mohit joshi:
just for clarity, the code given has constructed a thread object without passing any runnable, ( and has also not made object of a subclass of Thread to provide implementation of run). So the default run method doesnot do anything.
Doesnt thread priority vary from 0 to 10, that is 11 different levels of it?? I remember reading this somewhere.

Priority ranges from 1 to 10, so only 10 levels.
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another Clarification:
From Java Threads by Scott Oaks and Henry Wong : page 138
"Every thread has a priority value that lies somewhere in the range between MIN_PRIORITY(which is 1) and MAX_PRIORITY( which is 10).....
...In addition, the virtual machine is allowed to create internal threads at a priority of 0, so that there are in effect 11 different priority levels for threads within the virtual machine."
From the above it looks like java programmers can create threads with priority 1 to 10, and cannot create threads with priority 0.
[This message has been edited by mohit joshi (edited December 09, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic