文档介绍:
Graphics
1
Graphics & Graphical Programming
Lecture 18 - Introduction to Threads
Graphics
2
Intro to Threads- Outline
What is a Thread?
Uses in Timing or Animation in Games and Simulation
Many systems are in fact multi-threaded - you do not always see or need to be aware of the system threads working in the background
Java has several systems threads for the Garbage Collector and for the AWT graphics system
Using Java inbuilt thread facilities
Graphics
3
What is A Thread?
Normally we think of our program as being a single thread of control
The CPU architecture has:
A program (compiled code)
A program counter
Program context - ie memory allocated, values in registers
A multi-threaded program has a set of “virtual CPUs”
Each has its own thread of control, linked to a separate program counter
Threads generally share the program code and memory allocation of a single program (unlike eg full “processes”)
Graphics
4
Cooperating threads
Several threads running the same program can usefully cooperate to help the user achieve an e
eg managing the windows system or mouse and keyboard
Think of threads as a way of conceptualising or managing a concurrent or parallel program that can effectively “do” several things at once
Graphics
5
Using Threads
Needs careful planning in the general case
Need to consider what each thread will do…
How will they be initialised?
How will they exchange information (eg shared variables)
Threads libraries are available mon programming languages like C or C++
Java has a threads system integrated into the language
Thread object or Runnable interface
Graphics
6
Java Threads
The Java Virtual Machine (JVM has several system threads that help your program run
Garbage Collector thread wakes up periodically and recovers memory no longer used by unreferenced objects
AWT graphics thread looks after the various events that can occur ina program such as mouse of keyboard ev