import java.applet.*; import java.awt.*; public class Threads2 extends Applet implements Runnable { int width, height; int i = 0; Thread t; // Executed when the applet is first created. public void init() { System.out.println("init(): begin"); width = getSize().width; height = getSize().height; setBackground( Color.black ); System.out.println("init(): creating thread"); t = new Thread( this ); System.out.println("init(): starting thread"); t.start(); System.out.println("init(): end"); } // Executed when the applet is destroyed. public void destroy() { System.out.println("destroy()"); } // Executed within the thread that this applet created. public void run() { System.out.println("run(): begin"); try { while (true) { System.out.println("run(): awake"); // Here's where the thread does some work ++i; if ( i == 10 ) { i = 0; } showStatus( "i is " + i ); System.out.println("run(): requesting repaint"); repaint(); System.out.println("run(): sleeping"); t.sleep( 1000 ); // interval given in milliseconds } } catch (InterruptedException e) { } System.out.println("run(): end"); } // Executed whenever the applet is asked to redraw itself. public void paint( Graphics g ) { System.out.println("paint()"); g.setColor( Color.green ); g.drawLine( width, height, i * width / 10, 0 ); } }