Java Reference
In-Depth Information
should allow up to 2500 tiny threads, but in practice, because the thread code
itself plus any memory allocated for objects a thread uses takes up memory too,
an OutOfMemoryError will usually occur far sooner.
8.4.2 Setting priorities
Every thread has an integer priority value between 1 and 10 that can be con-
trolled using methods in the Thread class. Generally, higher priority threads
can be expected to be given preference by the thread scheduler over lower pri-
ority threads. However, the implementation of thread scheduling is left up to
the JVM implementation. This lack of specificity provides maximum flexibil-
ity to JVM designers since Java can be implemented on platforms with limited
speed and resources and also on platforms with multiple processors and extensive
resources.
The JVM implementation must work within the native platform's multithread-
ing capabilities, which might or might not include native multithreading features.
Even among host operating systems that natively support multiple threads, the
details of that support are sure to be different among different operating systems
and perhaps among different hardware platforms. About all that can be said for
certain is that higher priority threads should receive preferential treatment by
the thread scheduler compared to threads with lower priority. However, if two or
more threads are waiting for processor resources, the thread scheduler may also
take into account how long the threads have been waiting. The highest priority
thread is perhaps likely to be the first to be scheduled, though not necessarily.
Over a long enough sampling time, higher priority threads will, on average, be
scheduled more often than lower priority threads, but that does not mean that
at any given time a lower priority thread might have control of the CPU while
a higher priority thread is waiting. In general, changing Java thread priorities is
not a reliable way to attempt to force one thread to always have preference over
another. (See Section 24.4 for a discussion of the real-time specification for Java,
which expands the number of priority levels to 28 and requires strict enforcement
of thread execution according to priority settings.)
With those caveats, you can get and set a thread's priority with the
getPriority() and setPriority() methods in the Thread class. The
Thread class defines three constants:
MIN - PRIORITY
NORM - PRIORITY
MAX - PRIORITY
The default priority is Thread.NORM - PRIORITY ,which is 5, although new
threads always inherit the priority value of the creating thread. The following
Search WWH ::




Custom Search