Sunday, February 13, 2011

Running and Startting Threads

When you construct an object derived from Thread, the run method is not auto matically called.
Ball b = new Ball ( ... ) ; // wont't run yet
you should call the start method in your object to actually start a thread .
b.start ( );

* Note *
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 .
* Note *

In the java programming language , a thread needs to tell the other threads when it is idle, so the other threads can grab the chance to execute the code in their run procedures. The usual way to do this is through the static sleep method . The run method of the Ball class uses the call to sleep ( 5 ) to indicate that the thread will be idle for the next 5 millisecond . After 5 millisecond , it will start up again , but in the meantime , other threads have a chance ti get work done .


From a desain point of view , it seems strange to have the class Ball extend the class thread. A ball is an object that move on the screen and bounce off the corner . Does the is - a rule for inherince apply here ? is a ball a thread ? not realy . Here , we are using inheritance strictly for techncal reasons. To get a thread you can control, you need a thread object with a run method . we might as well add that run method to the class whose methods and istance field the run method uses. Therefore, we make Ball a child class of Thread. Also , it make the example easy to grasp : every Ball object that you constuct and start executes its run method in its own thread. You will see later in this chapter how you can use the runnable interface to avoid extending the Thread class
Figure 1 -2 the UI and ball threads

***********************************************************************************

import java.awt . *;
import java.awt.event.*;
import java.swing.*;

public class BounceThread
{  public static void main ( string [ ] args )
   { JFrame frame = new BounceThreadFrame ( ) ;
      frame.show ( ) ;
}
}
class BounceThreadFrame extends JFrame
{ public bounceThreadFrame ( )
   { setSize ( 300, 200 );
      setTitle ( "bounce" ) ;

addWindowListener ( new WindowAdapter ( )
    { public void windowClosing ( windowEvent e )
       { System.exit ( 0 ) ;
}
} ) ;

Container contentPane = getContentPane ( ) ;
canvas = new JPanel ( );
contenPane.add ( canvas, " Center" ) ;
JPanel p=  new JPanel ( );
addButton ( p, " Start ",
      new ActionListener ( )
  { Public Void ActionPerformed ( ActionEvent evt )
          { Ball b = new Ball ( canvas ) ;
                     b.start ( );
}
} ) :

adddButton ( p, "close"
 new ActionListener ( )
{ Public Void actionPerformed ( ActionEvent evt )
{ canvas.setVisible ( false );
     System.exit ( 0 ) ;
}
}) ;
contenPane.add (, "South" ) ;
}
public void addButton ( Container c , string title,
ActionListener a )
{ JButton b = new Jbutton ( title ) ;
  c.add ( b ) ;
   b.addActionListener ( a );
}
private JPanel canvas ;
}
class Ball extends Thread
{ public Ball( JPanel b ) { box = b; }

public void draw ( )
{Grapihics g = boxgetGraphics ( );
  g.filloval ( x,y, XSIZE, YSIZE );
  g.dispose ( );
}
public void move ( )
{ if ( ! box.isViseble ( ) ) return;
{ Graphics g = box getGraphics ( ) ;
   g.setXORMODE ( box.getBackground () );
   g.filloval ( x, y, XSIZE, YSIZE);
   x + = dx ;
   y + = dy ;
dimension d = box.getSize ( )
if ( x < 0 )
   { x = 0; dx = -dx ; }
    if ( x + XSIZE >= d.width )
     { x = d.width - XSIZE; dx = -dx; }
if ( y < 0 )
{ y = 0; dy = -dy; }
if ( y + YSIZE >= d.height )
 { y = d.height - YSIZE; dy = -dy; }
  g.fillovall ( x,y, XSIZE, YSIZE );
  g.dispose ( );
}
public void run ( )
{ try 
  { draw ( );
   for ( int i = 1; i <= 1000; i++)
{ move ( );
 try { Thread.sleep ( 5 ) ; }
catch ( InterruptedException e ) { }
 }
}
private JPanel box ;
private static final int XSIZE = 10 ;
private ststic final int YSIZE =  10;
private int x = 0 ;
private int y = 0 ;
private int dx = 2 ;
private int dy = 2 ;
}
***********************************************************************************

No comments:

Post a Comment