JavaRanch Home    
 
This page:         last edited 23 October 2013         What's Changed?         Edit
READ ONLY

Code Barn Hello Thread   

You must turn on Java for applets to work.

Explanation of HelloThread? applet

Number of classes: 1 (HelloThread?.class)

What it does: starts a Thread which loops through a run method 10 times, and displays a number for each loop iteration.

To run code as a thread requires two things:

1) a JavaDoc:java.lang.Thread object (this is the worker)

2) a JavaDoc:java.lang.Runnable object with a run method (the job for the worker)

The HelloThread?.class (an applet subclass) implements the Runnable interface. Run() is the only method required by classes which implement Runnable. The run() method will be called by the Thread object.

The class which implements Runnable is the "target" of the Thread object.

So in this example, two different objects are used, one for the Thread, and one for the Runnable (target) of the Thread. Using two different objects is the most common way of having code run as a thread, although you can subclass Thread and use the Thread object itself as the Runnable object (Thread implements Runnable)

What actually happens in this applet:

Two instance variables are declared: one for the Thread object, and one for the GUI Label (used to display a number for each loop the Thread object makes in the run() method of the applet).

init() is called:

  • creates and adds the Label to the applet

  • creates a new Thread object, giving it 'this' (itself) as a target

  • calls the Thread object's start() method (which causes the Thread to call its target's run() method)

run() is called:

  • has a for loop which loops 10 times and displays the loop count in the Label.

  • puts the Thread to sleep for one second, to slow it down. (otherwise it would loop 10 times too quickly for you to see)




import java.applet.Applet ;
import java.awt.Label ;
    
public class HelloThread extends Applet implements Runnable 
{
    //declare instance variables
    private Thread myThread ;
    private Label myLabel ;
    
    public void init() 
    {
        //init the variables
        myThread = new Threadthis );
        //myThread has been created, but is not yet "alive"
    
        myLabel = new Label"Thread info goes here" );
    
        //set up the GUI (it's tiny) 
        add( myLabel );
    
        //start the Thread
        myThread.start();
        // this causes the Thread to become runnable
        // and when it is Running, it will execute the run method 
    } // close init
    
    public void run() 
    {
        // this run method will repeat 10 times
        for ( int i = 0 ; i < 10 ; i++ ) 
        {
            myLabel.setText( "Run method loop: " + i );
            // then put it to sleep for 1 second to slow it down
    
            try 
            {  // sleep must always be in a try/catch
                Thread.sleep( 1000 ) ;
            }
            catch ( InterruptedException e ) 
            {
            }
        } // close for loop
    } // close run method 
// close applet



CodeBarnApplets


JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton