• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread priority

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1) What does the integer number for thread priority mean in here ? Does 10 mean that it will be run at 10th number and 3 mean at 3rd number ? Or does 10 mean higher priority than 3 ? If 10 means higher priority than 3, then how higher is that ? If I put 20 instead of 10, what difference does it make ? If I want "thread t" have higher priority than "thread t1" , do I provide higher number as an argument in setPriority(number) ? If I set t.setPriority(2) and t1.setPriority(1) that means thread t has higher priority. Why do I need to make any any higher number than 2 for thread t when thread t1 is set with priority number 1 ? What difference does higher number make for setPriority() , since number is already higher by 1 ?

2) public static void main(String... args)
Is this definition of main() correct ? I have taken this example from mock tests. I think only main(String[] args) is the correct way of declaring main()

Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12340
38
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not an expert on threads by any means, but from the java.lang.thread api:

"Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority."

So, I would guess that a thread with a priority of 10 would run, and then when it doesn't have anything to do, a thread with priority 3 would run. You may have a situation where you need more granularity than just two priorities, and are thus provided with a range. I believe that if you only need two priorities, it doesn't matter if you use (1 and 2) or (7 and 94) - assuming those are allowed values.

2) I believe you are allowed to use (String...args) in main, but the best way to be sure is to try it and see.


*note - I edited your original post to use code tags. They make it much more readable.
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nirjari,

Thread priorities helps in determining which Thread will move from the runnable state to the running state when two or more threads are fighting for the cpu's attention. In general a thread with priority 10 will always get preference then a thread of priority 3. Plus at any given time a Thread in the running state has a priority which is either greater than or equal to all other Threads in the running state.

Moreover in general threads have priorities from 1-10, although in some JVMs it is from 1-5. So you cant put a priority of 20 to one of them. There are also some constants that you can use instead of integer values like



Plus thread priorities also play a deciding factor in thread scheduling in some JVMS...



And regarding the main()..its just a var-args definition of the usual main(String[] args) syntax and is actaully equivalent to String[] args and a perfectly lega one too!!!
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the available priorities of threads vary between operating systems, so using priorities sacrifices portability.

I think this discussion would sit better on our threads forum: moving.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your going to use Thread priority's you really need to look at your underlying OS , thread lib documentation. There not something you'd commonly use.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!