• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setPriority(int i) method of Thread

 
Mimi Chow
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Sun Guoqiao's Mock Exam3:

The question is: What is the output of trying to compile and run the following code?
The Options are:
A: 5 1
B: 5 5
C: 1 1
D: The code compiles but throws runtime
exception at //1 since you cannot setPriority after start().
The ANSWER is: A
The explaination is:
The setPriority method should be called before the thread has been started in order to take effect. You can call setPriority method after the thread starts though, where the calling does not affect the thread priority.
My Question is: If calling setPriority after the thread starts doesn't take effect, why does getPriority() on line 2 returns 1 instead of 5? Can someone kindly explain?
 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q : Can setPriority() change the priority of an already up and running thread ?
Ans: Yes! It can change! As long as the currently running thread has the rights to the change the priority if the thread it desires to change.
(There is some Accesscheckbeing done in the setPriority call- Look into API)
Tried a simple code .. by putting a run method in ur above Question. Worked fine and prints 5,1
Jennifer
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic