




























































































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!





























































































NOTE: In many programming languages, you have to use an external thread package to do multithreaded programming. The Java programming language builds in multi- threading, which makes your job much easier.
What Are Threads?
NOTE: If you carefully look over the code at the end of this section, you will notice the call canvas.paint(canvas.getGraphics()) inside the move method of the Ball class. That is pretty strange—normally, you’d call repaint and let the AWT worry about getting the graphics context and doing the painting. But if you try to call canvas.repaint() in this program, you’ll find out that the canvas is never repainted since the addBall method has completely taken over all processing. In the next program, where we use a separate thread to compute the ball position, we’ll again use the familiar repaint.
NOTE: Since most computers do not have multiple processors, the Java virtual machine (JVM) uses a mechanism in which each thread gets a chance to run for a little while, then activates another thread. The virtual machine generally relies on the host operating sys- tem to provide the thread scheduling package.
CAUTION: Do not call the run method directly—start will call it when the thread is set up and ready to go. Calling the run method directly merely executes its contents in the same thread—no new thread is started.
TIP: There are a number of static methods in the Thread class. They all operate on the current thread, that is, the thread that executes the method. For example, the static sleep method idles the thread that is calling sleep.
Event dispatch thread
Ball thread
move
sleep
move
sleep
start
The Runnable Interface
class Ball extends Thread { public void run() { try { for (int i = 1; i <= 1000; i++) { x += dx; y += dy;
... canvas.repaint(); sleep(5); } } catch (InterruptedException exception)
private Component canvas; private int x = 0; private int y = 0; private int dx = 2; private int dy = 2; }
class Animation extends JApplet implements Runnable {
... public void run() { // thread action goes here } }
class Animation extends JApplet implements Runnable {
... public void start() { runner = new Thread(this); runner.start(); } ... private Thread runner; }
public void run() {
... while ( no request to terminate && more work to do ) { do more work } // exit run method and terminate thread }
public void run() { try {
... while ( more work to do ) { do more work } } catch(InterruptedException exception) { // thread was interrupted during sleep or wait } finally
cleanup, if required } // exit run method and terminate thread }
while ( !interrupted() && more work to do ) { do more work }
NOTE: Curiously, there are two very similar methods, interrupted and isInterrupted. The interrupted method is a static method that checks whether the current thread has been interrupted. (Recall that a thread is inter- rupted because another thread has called its interrupt method.) Further- more, calling the interrupted method resets the “interrupted” status of the thread. On the other hand, the isInterrupted method is an instance method that you can use to check whether any thread has been interrupted. Calling it does not change the “interrupted” status of its argument.
try { sleep(delay); } catch (InterruptedException exception) {