Chapter 6: Thread Prioritization

Chapter 6 - Thread Prioritization

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Thread States
While a thread is alive , it is in one of several states. On a single-processor system, only one thread is actually running at a time. The other threads are either blocked or are ready-to-run and waiting for a turn on the processor. Figure 6.1 lists the various states that an active thread can be in.
Figure 6.1: Thread states.
The thread scheduler controls which one of the ready-to-run threads is actually running on the processor. Only one thread is actually in the running state at any given time. All the other threads that are ready-to-run wait to be picked by the thread scheduler. Thread priority helps the thread scheduler choose which of the ready-to-run threads should be run next .
Threads can be blocked in one of four states. When a thread executes Thread.sleep() , it blocks until the specified number of milliseconds passes or until it is interrupted by another thread. When a thread encounters a wait() statement (see Chapter 8, Inter-thread Communication, for more on the wait/notify mechanism), it blocks until it is notified, interrupted, or the specified number of milliseconds elapses (if a timeout was specified).
There are many ways a thread can block waiting on different I/O methods . One common way is the read() method of InputStream . This method blocks until a byte of data is read from the stream. It can block indefinitely, and no timeout can be specified.
A thread can also block waiting to acquire exclusive access to an objects lock. The synchronized statement and synchronized method modifier (see Chapter 7, Concurrent Access to Objects and Variables, for more on synchronization) are used to control concurrent access by more than one thread to a section of code. A thread will block on synchronized until it gets the specified lock. It can block indefinitely, and no timeout can be specified.
Notice that not all blocked states are interruptible. When a thread is blocked waiting to read a byte of data from an InputStream , it will not respond to interrupts (see Chapter 15, Breaking Out of a Blocked I/O State, for some techniques for dealing with this). When a thread is blocked waiting to get the lock required by a synchronized statement, it also will not respond to interrupt requests (see Chapter 17, BooleanLock Utility, for a technique that deals with long waits for locks).
When conditions change and a thread is no longer blocked, the thread moves to the ready-to-run state. It remains there until selected to run by the thread scheduler. Figure 6.2 shows how threads transition from one state to another.
Figure 6.2: Thread state transition diagram.
Only one thread is actually running at a time, and it eventually blocks, yields, or is forcibly swapped off the processor by the thread scheduler. If it blocks, it remains blocked until some condition changes. When it finally unblocks, it moves to the ready-to-run state. When new threads are started, they are put into the ready-to-run state.
When the running state is open and there is at least one thread in the ready-to-run state, the thread scheduler chooses one from the pool of ready-to-run threads and transitions it to the running state. Threads of higher priority are more likely to be chosen than threads of lower priority, but the exact behavior is dependent on the VM and the operating system.
  Tip These six states are not officially defined in Java, but are a useful tool for understanding thread behavior. The thread state transition diagram in Figure 6.2 is only a model that is helpful in imagining the inner workings of the VM. Most VM implementations use the underlying operating systems threads and thread scheduler. This model helps conceptualize what might be going on inside the VM, but it does not map directly to a real implementation.

Toc


Java Thread Programming
Java Thread Programming
ISBN: 0672315858
EAN: 2147483647
Year: 2005
Pages: 149
Authors: Paul Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net