Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 2.77 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.applet.*;
  2. import java.awt.*;
  3.  
  4. public class Threads1 extends Applet implements Runnable {
  5.  
  6.    int width, height;
  7.    int i = 0;
  8.    Thread t = null;
  9.    boolean threadSuspended;
  10.  
  11.    // Executed when the applet is first created.
  12.    public void init() {
  13.       System.out.println("init(): begin");
  14.       width = getSize().width;
  15.       height = getSize().height;
  16.       setBackground( Color.black );
  17.       System.out.println("init(): end");
  18.    }
  19.  
  20.    // Executed when the applet is destroyed.
  21.    public void destroy() {
  22.       System.out.println("destroy()");
  23.    }
  24.  
  25.    // Executed after the applet is created; and also whenever
  26.    // the browser returns to the page containing the applet.
  27.    public void start() {
  28.       System.out.println("start(): begin");
  29.       if ( t == null ) {
  30.          System.out.println("start(): creating thread");
  31.          t = new Thread( this );
  32.          System.out.println("start(): starting thread");
  33.          threadSuspended = false;
  34.          t.start();
  35.       }
  36.       else {
  37.          if ( threadSuspended ) {
  38.             threadSuspended = false;
  39.             System.out.println("start(): notifying thread");
  40.             synchronized( this ) {
  41.                notify();
  42.             }
  43.          }
  44.       }
  45.       System.out.println("start(): end");
  46.    }
  47.  
  48.    // Executed whenever the browser leaves the page containing the applet.
  49.    public void stop() {
  50.       System.out.println("stop(): begin");
  51.       threadSuspended = true;
  52.    }
  53.  
  54.    // Executed within the thread that this applet created.
  55.    public void run() {
  56.       System.out.println("run(): begin");
  57.       try {
  58.          while (true) {
  59.             System.out.println("run(): awake");
  60.  
  61.             // Here's where the thread does some work
  62.             ++i;  // this is shorthand for "i = i+1;"
  63.             if ( i == 10 ) {
  64.                i = 0;
  65.             }
  66.             showStatus( "i is " + i );
  67.  
  68.             // Now the thread checks to see if it should suspend itself
  69.             if ( threadSuspended ) {
  70.                synchronized( this ) {
  71.                   while ( threadSuspended ) {
  72.                      System.out.println("run(): waiting");
  73.                      wait();
  74.                   }
  75.                }
  76.             }
  77.             System.out.println("run(): requesting repaint");
  78.             repaint();
  79.             System.out.println("run(): sleeping");
  80.             t.sleep( 1000 );  // interval given in milliseconds
  81.          }
  82.       }
  83.       catch (InterruptedException e) { }
  84.       System.out.println("run(): end");
  85.    }
  86.  
  87.    // Executed whenever the applet is asked to redraw itself.
  88.    public void paint( Graphics g ) {
  89.       System.out.println("paint()");
  90.       g.setColor( Color.green );
  91.       g.drawLine( width, height, i * width / 10, 0 );
  92.    }
  93. }