Lecture 10: Threads

Overview

Programming with threads is the ability to program several tasks to run simultaneously. You would want to do this when you want to respond to different events in a timely manner. Threads are used by Window systems to listen for user events such as mouse clicks and requests to abort actions.

Each thread is represented by a Java object java.lang.Thread. Threads are created using new, started using the start method, and run until the run method exits, or the thread is stopped. The thread, when run, runs either the run method of its own body, or the run method of any object which implements the java.lang.Runnable interface, depending on how the Thread was constructed.

Examples

The details

A thread is an operating system abstraction for a unit of schedulable activity and its context. Most notably, a pointer in memory to the next instruction to be executed, and the memory control registers to correctly map the virtual addresses of the process. An instance of this abstraction is represented in Java by an instance of a Thread object. An instance of a Thread is the control for an actual operating system thread. Invoking the start method of the thread instance will place the operating system thread on a schedule of CPU cycles.

The operating system thread will begin running either its own run method, or the run method of the object passed at Thread construction. The data environment will be shared with other threads which are running methods in this object. Local variables are not shared. Each thread has private local variables. Instance and class variables are shared.

When the start method returns, there will now be a new process for the operating system to schedule, running virtually in parallel with the other tasks On truth, however, the many threads are sequentially given time on a single CPU, and the amount of time and sequencing is called scheduling. According to the sceduling policy of the thread implementation, it maybe that some threads never get processor time, and the method assigned to that thread will not advance towards completion.

The Java reference says only that threads with a higher priority should receive more CPU cycles than threads with lower priority. Priority is a thread property and can be set by the Thread interface. But the reference does not say must, only should, and platforms and Java virtual machine implementations will differ. Threads is an area where great care must be exercised if a Java program is to be cross-platform. The Java Threads book by O'Reilly is a good source for the current situation. In summary, that book finds that:

Since the NT thread scheduling mechanism is fair, if more than one thread shares the current highest priority, NT will make sure that both threads get approximately equal time on the CPU. Unix schedulers will not pull a thread from the CPU only to replace it with a thread of equal priority. Unix threads are selfish and will run to completion, or until an explicit yield, without yielding to any other thread of the same priority.

Both these policies are in agreement with the broad statement of the Java Reference Manual.

A thread is runnable or active if it has been started, has not been stopped, and is not blocked. Blocking occurs either while the thread is in a system call waiting for file access or user input, or if:

A stopped thread cannot be restarted or reused.