The determination of when threads run is left up to
the JVM implementation to allow maximum flexiblity for the various
applications of Java.
Threads are assigned priorties between 1 (lowest)
to 10 (highest). A thread can set its own thread up or down in priority.
Three standard priority values are defined:
MIN_PRIORITY
NORM_PRIORITY
MAX_PRIORITY
For example,
...
Thread threadX = new Thread(this);
threadX.setPriority (Thread.NORM_PRIORITY + 1);
threadX.start ();
..
For threads at the same priority, once one of them
starts running, it continues until one of the following happens:
- Sleeps - sleep()
or wait()
called
- Waits for a lock in a synchronized method
- Blocks on I/O such as a read()
- Yields control with a yield()
call
- Terminates - e.g. returns from run()
As discussed earlier,
preemptive or time-slicing context switching, which is available
on some JVMs, also will move threads of the same priority in and
out of the virtual processor
Latest update: Nov. 8, 2004
|