File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes setPriority(int i) method of Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "setPriority(int i) method of Thread" Watch "setPriority(int i) method of Thread" New topic

setPriority(int i) method of Thread

Mimi Chow

Joined: Feb 05, 2002
Posts: 1
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

Joined: Nov 30, 2001
Posts: 102
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
I agree. Here's the link:
subject: setPriority(int i) method of Thread
jQuery in Action, 3rd edition