JavaRanch Home    
 
This page:         last edited 09 October 2007         What's Changed?         Edit
PROTECTED

Code Barn Hello Thread Two   

You must turn on Java for applets to work.

Explanation of HelloThreadTwo? applet

Number of classes: 1 (HelloThreadTwo?.class)

What it does: starts three threads; each thread will loop through the same run method 10 times and display its own name as it's running. You can use it to see the order in which the three Thread objects actually run. Then compare this with the NoSleep? variation of HelloThreadTwo?, which starts the Threads but does not put them to sleep.

To run code as a thread requires two things:

1) a Thread object (this is the WORKER)

2) a Runnable object with a run method (the JOB for the WORKER)

The HelloThreadTwo?.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, four different objects are used, three uniqueThread objects/instances, and one for the Runnable (target) of the Thread (which is the Applet itself). Using different objects (for the Thread and the target Runnable) is the most common way of having code run as a thread.

What actually happens in this applet:

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

init() is called:

  • creates and adds the List to the applet

  • creates three new Thread objects giving each one 'this' (the applet) 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 Thread name in the List.

  • puts the Thread to sleep for a half-second, to slow it down.

What to look for: notice the order the Threads run in. Because they are "forced" to sleep, all threads will appear to be taking turns, although the sequence may not be perfect (1,2,3,1,2,3,1,2,3, etc.). Most importantly, compare the output from this applet to the output from the NoSleep variation. Even better, see if you can run this on both a PC and a Macintosh, to see the effects of different Thread scheduling.



CODE


import java.applet.Applet ;
import java.awt.List ;
    
public class HelloThreadTwo extends Applet implements Runnable 
{
    // declare instance variables
    private Thread threadOne ;
    private Thread threadTwo ;
    private Thread threadThree ;
    
    private List myList ;
    
    public void init() 
    {
        // init the thread variables
        threadOne = new Threadthis );
        threadTwo = new Threadthis );
        threadThree = new Threadthis );
    
        // now give each Thread a name
        threadOne.setName( "Bert" );
        threadTwo.setName( "Marley" );
        threadThree.setName( "Lucky" );
        // threads have been created, but are not yet "alive"
    
        // create the list
        myList = new List"Thread info goes here" );
    
        //set up the GUI (just the list)
        add( myList );
    
        //start the Threads
        threadOne.start();
        threadTwo.start();
        threadThree.start();
        // this causes the Threads to become runnable
        // ALL of them will be running through this applet object's
        // run method, but each Thread will be running at a different
        // moment. They will all sleep for a half-second (500 milliseconds) 
    } // close init
    
    public void run() 
    {
        // this run method will repeat 10 times
        for ( int i = 0 ; i < 10 ; i++ ) 
        {
            myList.addItem( Thread.currentThread().getName() + " is running" );    
            try 
            {
                Thread.sleep( 500 );
            }
            catch (InterruptedException e) 
            {
            }
        } // close for loop
        myList.addItem( Thread.currentThread().getName() + " is done" );
    } // close run
// close applet



CodeBarnApplets


JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton