













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














Modern operating systems are multiprocessing
Appear to do more than one thing at a time
Three general approaches:
Cooperative multiprocessing Preemptive multiprocessing Really having multiple processors
Allows you to do more than one thing at once
Play music on your computer’s CD player, Download several files in the background, while you are writing a letter
Multithreading is essential for animation
One thread does the animation Another thread responds to user inputs
A Thread is a single flow of control
When you step through a program, you are following a Thread
Your previous programs all had one Thread
A Thread is an Object you can create and control
A Thread can be in one of four states:
Ready: all set to run Running: actually doing something Waiting, or blocked: needs something Dead: will never do anything again
State names vary across textbooks
You have some control, but the Java scheduler has more
ready
waiting
start running dead
class Animation extends Thread { public void run( ) { code for this thread } Anything else you want in this class } Animation anim = new Animation( ); A newly created Thread is in the Ready state To start the anim Thread running, call anim.start( ); start( ) is a request to the scheduler to run the Thread --it may not happen right away The Thread should eventually enter the Running state
class Animation implements Runnable {…} The Runnable interface requires run( ) This is the “main” method of your new Thread Animation anim = new Animation( ); Thread myThread = new Thread(anim);
To start the Thread running, call myThread.start( ); You do not write the start() method—it’s provided by Java As always, start( ) is a request to the scheduler to run the Thread--it may not happen right away
class Animation extends Thread { public void run( ) { while (okToRun) { ... } } }
Animation anim = new Animation( ); anim.start( );
class Animation extends Applet implements Runnable { public void run( ) { while (okToRun) { ... } } }
Animation anim = new Animation( ); Thread myThread = new Thread(anim); myThread.start( );
Suppose you set up Buttons and attach Listeners to those buttons...
…then your code goes into a loop doing the animation…
…who’s listening?
Not this code; it’s busy doing the animation
sleep( ms ) doesn’t help!
Create your buttons and attach listeners in your first (original) Thread
Create a second Thread to run the animation
Start the animation
The original Thread is free to listen to the buttons
However,
Whenever you have a GUI, Java automatically creates a second Thread for you You only have to do this yourself for more complex programs
Don’t use the deprecated methods!
Instead, put a request where the other Thread can find it
boolean okToRun = true; animation.start( );
public void run( ) { while (controller.okToRun) {…}
What gets printed as the value of k? This is a trivial example of what is, in general, a very difficult problem