views:

184

answers:

4

Hi,

I have a doubt

There are 10 different threads in runnable state. Each having priority 1 to 10. How does the CPU schedules or executes these threads?

Thanks, Ravi

+1  A: 

Mainstream Java implementations use "native threads", which means that thread scheduling is done through the operating system. Java thread priorities simply map to OS-specific values. You should read your OS documentation to figure out what those levels mean, though. :-)

Chris Jester-Young
I don't believe this is always the truth. I believe Java can emulate native threads if they don't exist on the platform.
Bill K
It's not _always_ the case, you're right: pre-1.2 JDK versions used green threads, IIRC. But, mainstream JVMs all use native threads these days, and it's not useful to talk about green-thread implementations, in my opinion.
Chris Jester-Young
Java uses native thread, when possible, not all the implementations have native thread, some other use "green" threads.
OscarRyz
A: 

The OS has a thread scheduler that will (using an algorithm) decide based on priority and a few other factors, which thread will be run next. If you have a multi-core system, then each CPU can take a thread for it's account.

There's also the fact that a thread gets a slot of time, and then gets switched out for another thread and has to wait its turn again.

But thread scheduling is an Operating System function.

I hope that gives you an answer to your question.

Tony
+3  A: 

Since when did this place replace google?

google search for Java thread scheduling, first result:

http://lass.cs.umass.edu/~shenoy/courses/fall01/labs/talab2.html

Bill K
+1, Is this any different than coworkers asking questions in person before searching for themselves?
confusedGeek
Most searches aren't this easy. It was simple keywords and the first response. Funnily, it was kind of interesting and goes into a good level of detail--anyone interested should probably read it.
Bill K
The problem with google is, if you don't know exactly what terms to search, the results are not very useful. Compare the search results: "Threads concept in Java" vs. "Java Thread scheduling" Asking in StackOverflow would yield better results because there is a human at the other end ( even when he just provide a link returned by gooogle )
OscarRyz
A: 

It is worth noting, that windows ignores raised priorities, unless you are Administrator and on Linux all priorities are ignored unless you are root.

Generally, playing with thread priorities is not very useful.

Peter Lawrey