Presentation is loading. Please wait.

Presentation is loading. Please wait.

Animate objects and threads

Similar presentations


Presentation on theme: "Animate objects and threads"— Presentation transcript:

1 Animate objects and threads
Outline: What is an animate object? What is a Thread? How are animate objects and Threads related? Apply these ideas: Paper-and-pencil exercise Threads program example Fundamentals of Software Development I Lecture 20

2 Animate objects Consider the following objects… Counter Timer
Has state and behavior Doesn’t do anything unless someone asks it to do so using its increment or reset method Timer Has state and behavior, much like a Counter Has a traditional, passive reset method But a Timer acts by itself! Its increment method is called by the Timer itself on a regular basis. Animate object Fundamentals of Software Development I Lecture 20

3 Threads In Day 1 we saw two prerequisites for computation:
Instructions for the computation must be present The instructions must be executed In Java, objects that execute instructions are called Threads Each Thread follows the instructions it is given No object can act except a Thread There can be many Threads executing concurrently How can an ordinary computer execute many Threads “at the same time”? Answer on next slide Fundamentals of Software Development I Lecture 20

4 Concurrent Threads A computer repeatedly:
Called CPU scheduling Round robin First-in, First-Out (FIFO) Shortest job first etc. A computer repeatedly: Picks a Thread Restores the system to the state when the Thread last ran Which statement was it doing? What values were bound to the objects/variables at that time? Runs the Thread for a little while Saves the state of the system The Threads appear to run “at the same time” if the time step is small enough Fundamentals of Software Development I Lecture 20

5 Runnable: connecting Animate objects and Threads
What are they? A Thread is, by definition, an instruction-follower An animate object is, by definition, an object with its own Thread (i.e., its own instruction-follower) What do they promise? The animate object promises to supply instructions The Thread promises to execute them How are they connected? through the Runnable interface (see next slide) Fundamentals of Software Development I Lecture 20

6 Using the Runnable interface
An animate object typically: Implements the Runnable interface Runnable specifies a run method Constructs and starts a Thread In its constructor or soon thereafter Exercise: Write a single expression that constructs a Thread and asks it to start The constructor needs an argument – the object doing the constructing Answer: (new Thread(this)).start() Supplies a method called run: public void run() run often contains a loop that goes for a long time The Thread: executes the animate object’s run method The animate object promises to supply instructions The Thread promises to execute those instructions Fundamentals of Software Development I Lecture 20

7 Demo: Threads example I’ll show you the Threads project Note:
There is a link to it on Angel (under Exercise link in the Schedule page) Note: Timer class: an animate object! Implements Runnable Constructs and starts a new Thread run method Creation of Timers in Threads constructor Interaction of Timers with Panels We’ll use this same example later to demo a GUI (graphical user interface). Ignore details of panels, etc. for now Interaction of Reset button and Timers Independent objects! What does (purposely) NOT work quite right? That is, what needs to be synchronized? Now do the Exercise on Animate Objects and Threads (associated item on today’s schedule) Fundamentals of Software Development I Lecture 20

8 Threads Threads There can be many Threads executing concurrently
Each Thread follows the instructions it is given No object can act except a Thread executes the animate object’s run method There can be many Threads executing concurrently Fundamentals of Software Development I Lecture 20


Download ppt "Animate objects and threads"

Similar presentations


Ads by Google