Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread priority

 
Kailash Bisht
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:
2. class Mosey implements Runnable {
3. public void run() {
4. for(int i = 0; i < 1000; i++) {
5. System.out.print(Thread.currentThread().getId() + "-" + i + " ");
6. } } }
7. public class Stroll {
8. public static void main(String[] args) throws Exception {
9. Thread t1 = new Thread(new Mosey());
10. // insert code here
11. }
12. }

Which of the following code fragments, inserted independently at line 10, will probably run
most (or all) of the main thread’s run() method invocation before running most of the t1
thread’s run() method invocation? (Choose all that apply.)

A. t1.setPriority(1);
new Mosey().run();
t1.start();

B. t1.setPriority(9);
new Mosey().run();
t1.start();

C. t1.setPriority(1);
t1.start();
new Mosey().run();

D. t1.setPriority(8);
t1.start();
new Mosey().run();

can you explain how option b is correct as the priority set is 9..??(as mentioned in d book )
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output for this code may vary from one JVM to another, as for some JVM, priority from HIGH to LOW is defined as 1 to 10, and in some JVM it is 10 to 1.
So, you cannot exactly predict the output for this case..
So, given that you have to choose one option, and for two options, priority is being set to 1..
So, you can assume that current JVM has 10 as the highest priority (Else both option having priority 1 would have been the answer)

Now, since 10 is the highest, 9 will be the second highest, and hence the answer..
 
Kailash Bisht
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:Output for this code may vary from one JVM to another, as for some JVM, priority from HIGH to LOW is defined as 1 to 10, and in some JVM it is 10 to 1.
So, you cannot exactly predict the output for this case..
So, given that you have to choose one option, and for two options, priority is being set to 1..
So, you can assume that current JVM has 10 as the highest priority (Else both option having priority 1 would have been the answer)

Now, since 10 is the highest, 9 will be the second highest, and hence the answer..


rohit thanks i got your point why priority 1 will be answers but thing is that in i am not getting how come 9 b answers...if 9 can then why not 8....???
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kailash Bisht wrote:
R. Jain wrote:Output for this code may vary from one JVM to another, as for some JVM, priority from HIGH to LOW is defined as 1 to 10, and in some JVM it is 10 to 1.
So, you cannot exactly predict the output for this case..
So, given that you have to choose one option, and for two options, priority is being set to 1..
So, you can assume that current JVM has 10 as the highest priority (Else both option having priority 1 would have been the answer)

Now, since 10 is the highest, 9 will be the second highest, and hence the answer..


rohit thanks i got your point why priority 1 will be answers but thing is that in i am not getting how come 9 b answers...if 9 can then why not 8....???

Your statement before and after but seems contradictory towards your understanding of what I wrote..
If 10 is the highest priority, then 9 will be higher in priority than 8..
So, the higher the priority, the higher will be the likelihood of the thread t1 being completed before main..
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kailash Bisht wrote:

can you explain how option b is correct as the priority set is 9..??(as mentioned in d book )



Please QuoteYourSources.

Henry
 
Kailash Bisht
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


now i got it...thanks..
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kailash Bisht wrote:

B. t1.setPriority(9);
new Mosey().run();
t1.start();

can you explain how option b is correct as the priority set is 9..??


R. Jain wrote:
Kailash Bisht wrote:
rohit thanks i got your point why priority 1 will be answers but thing is that in i am not getting how come 9 b answers...if 9 can then why not 8....???

Your statement before and after but seems contradictory towards your understanding of what I wrote..
If 10 is the highest priority, then 9 will be higher in priority than 8..
So, the higher the priority, the higher will be the likelihood of the thread t1 being completed before main..


The reason why choice B is correct, actually has nothing to do with priority. Notice that the main thread calls the run() method, and finishes the run() method, before it even starts the other thread. It actually doesn't matter what the priority of the other thread is set to. It won't be started until main thread is done with its output.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:Output for this code may vary from one JVM to another, as for some JVM, priority from HIGH to LOW is defined as 1 to 10, and in some JVM it is 10 to 1.


Not exactly sure what you mean by this. The Java thread priorities are mapped to the thread priorities of the underlying threading system, which can be anything. However, the Java thread priorities (the value seen on the Java side) are defined to be comparable. This means that the higher priority is defined to be greater in value than the lower priority -- it may be different once it is mapped (and then scheduled by the OS), but the Java program don't see those priority values.

R. Jain wrote:
So, given that you have to choose one option, and for two options, priority is being set to 1..
So, you can assume that current JVM has 10 as the highest priority (Else both option having priority 1 would have been the answer)


Actually, I believe this is one of those K&B questions that have more than one correct answer.... In this case, the correct answers should be A, B, and C.

which brings this back to my first post ... please QuoteYourSources (or at least confirm it).

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic