Home : Course Map : Chapter 8 : Java : Supplements :
Threads in the Processor
Course Map
Chapter 8

Threads Overview
  Demo 1   Demo 2
Stopping Threads
Thread Tasks
 Demo 3   Demo 4  
  Demo 5

  Demo 6

Task Splitting
  Demo 7

  Demo 8

  Demo 9

More Thread


Java2D Animation
  Demo 1 
Processor View
More Concurrency
  Demo 2  Demo 3 

     About JavaTech
     Codes List
     Topic Index
     Course Guide
     What's New

We can use a simple crude model of a standard processor to illustrate the concepts involved in threading.

When a program runs, the registers contain the current state of execution, which includes a pointer to the memory address of the next instruction.

When the program flow jumps to another location, as when a method call occurs, the current register values are saved on a stack and a new next-instruction-address, as well as other register values are loaded.

When the flow returns to the original location, the old register values are popped off the stack and loaded back into the registers.

Note: to allow for many layers of method calls, the stack should be as large as possible.

Multi-threading involves maintaining more than one stack and switching between them.

This diagram illustrates multi-threading:

A separate stack in this case is maintained for each thread.

Suppose Thread 1 represents the initial process flow. At some point, determined by the multi-processing architecture, the processing is stopped, and the current registers are saved on the Thread 1 stack.

The top of the Thread 2 stack replaces the registers and then the processing continues.

At a later point in time, the processing stops again and the current registers are saved on the Thread 2 stack. The Thread 3 stack values are popped off and put into the registers and then processing continues.

This switching in and out of the different thread stacks proceeds in this manner until the program finishes. Individual threads may finish during the processing and new ones created.

The details of particular hardware and virtual processor will vary but the general principle here of saving the state of a process and replacing it with the state of a separate process, is the essence of threading (and multi-processing) on a single processor system.

Note that the JVM does not use a model based on multiple registers. Instead, the JVM uses a stack frame processing model. Whenever a method is invoked, its state (values of the method variables and a pointer to the next instruction) is placed on the stack. If this method in turn invokes another method, then the new method frame is placed on top of the stack and it executes.

Each thread has a stack of such a method frames as one method invokes another. The total frame stack will be saved when a new thread displaces it. .


Last update: Feb.28.04

  Demo 1
Hist. Adapt Range
  Demo 2
Sorting in Java
  Demo 3
Histogram Median
  Demo 4
  Demo 5
Error Bars
  Demo 6

Least Squares Fit
  Demo 1
Fit to Polynomial
  Demo 2
Fit Hist Errors
  Demo 3
  Demo 4
  Demo 5

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
22 23 24

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.