aspose file tools*
The moose likes Threads and Synchronization and the fly likes thread priority Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "thread priority" Watch "thread priority" New topic
Author

thread priority

Kailash Bisht
Greenhorn

Joined: Sep 03, 2012
Posts: 5
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

Joined: Aug 11, 2012
Posts: 375
    
    1

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

Joined: Sep 03, 2012
Posts: 5
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

Joined: Aug 11, 2012
Posts: 375
    
    1

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kailash Bisht
Greenhorn

Joined: Sep 03, 2012
Posts: 5


now i got it...thanks..
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: thread priority