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

Code Barn Hello Thread Two No Sleep   

You must turn on Java for applets to work.

Explanation of HelloThreadTwoNoSleep? applet

Number of classes: 1 (HelloThreadTwoNoSleep?.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 Sleep variation of HelloThreadTwo?, which puts each Thread to sleep in the run() method.

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 unique Thread 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.

What to look for: notice the order the Threads run in. Without being "forced" to sleep, the Threads will not necessarily take turns. If you can run this applet on both a Macintosh and a PC, you might see a DRASTIC difference. On a PC, thread scheduling often works the way you might hope: the Threads appear to take turns, more or less. But on a Macintosh, one Thread might just stay in until it has finished!

WARNING: turn-taking is NOT built into Java! The responsibility is for you, the programmer. So while you might get good behavior on a PC, you can't guarantee it across all Java platforms. To guarantee fair Thread behavior (proper turn-taking) you must use a strategy which employs yield() and/or sleep(). So if this NoSleep? example runs on your machine exactly like the ThreadTwo? example (which DOES sleep), you're lucky. Nothing programmed in the Java applet is making that happen.

Bottom Line: If you're just starting with Threads, ALWAYS USE SLEEP in your run() method to be safe. Later, as you become more experienced with Threads you may use more sophisticated scheduling strategies.


import java.applet.Applet ;
import java.awt.List ;
public class HelloThreadTwoNoSleep 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
        // 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. 
    } // 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" );    
        } // close for loop
        myList.addItem( Thread.currentThread().getName() + " is done" );
    } // close run
// close applet                                                                               


JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton