One way of implementing a thread is to create a class that extends from
the java.lang.Thread class, and that then overrides the run()
method.
Here's an example of how to extend from class Thread:
package threads;
public class CountDownThread extends Thread
{
public CountDownThread(String name, int countDown,
CountDownProgress progress)
{
super(name);
m_countDown = countDown;
m_progress = progress;
}
// The method that does the work of the thread
public void run()
{
m_progress.started(getName() +
": started (counting from " +
m_countDown + ")");
try
{
for (int i = m_countDown; i >= 0; i--)
{
sleep(1000); // Sleep for 1 second
// Audible 'tick'
m_progress.tick(getName() + ": " + i);
}
}
catch (InterruptedException e)
{
// Ignore
}
finally
{
m_progress.done(getName() + ": done!");
}
}
/////// Private data ///////
private int m_countDown;
private CountDownProgress m_progress;
}
|
Notice that it uses a CountDownProgress, which is an interface:
package threads;
/**
* Interface to provide a simple progress
* for the CountDownThread.
*/
public interface CountDownProgress
{
public void started(String message);
public void tick(String message);
public void done(String message);
}
|
and here's a program to test it out:
package threads;
import java.util.Random;
public class CountDownThreadTester implements CountDownProgress
{
/**
* Main entry point.
*
* Arguments: list of thread names to use.
*/
public static void main(String[] args)
{
Random random = new java.util.Random();
CountDownThreadTester tester = new CountDownThreadTester();
int threadCount = 5;
for (int i = 1; i <= threadCount; i++)
{
// Generate a random number between 5 and 10
int countDown = random.nextInt(5) + 5;
CountDownThread thread =
new CountDownThread("Thread " + i, countDown, tester);
thread.start();
}
}
public void started(String message)
{
System.err.println(message);
}
public void tick(String message)
{
System.err.println(message);
}
public void done(String message)
{
System.err.println(message);
}
}
|
In other words, it creates 5 threads, telling each to count down starting
from a value somewhere between 5 and 10.
Note that the above test program implements the CountDownProgress
interface, and outputs the resulting messages to System.err --
intentional, because System.err is not buffered.
When I ran this, it produced the following output:
Thread 1: started (counting from 7)
Thread 2: started (counting from 7)
Thread 3: started (counting from 6)
Thread 4: started (counting from 7)
Thread 5: started (counting from 6)
Thread 1: 7
Thread 5: 6
Thread 4: 7
Thread 3: 6
Thread 2: 7
Thread 1: 6
Thread 5: 5
Thread 3: 5
Thread 4: 6
Thread 2: 6
Thread 1: 5
Thread 3: 4
Thread 5: 4
Thread 4: 5
Thread 2: 5
Thread 1: 4
Thread 2: 4
Thread 4: 4
Thread 5: 3
Thread 3: 3
Thread 1: 3
Thread 5: 2
Thread 4: 3
Thread 2: 3
Thread 3: 2
Thread 1: 2
Thread 2: 2
Thread 4: 2
Thread 5: 1
Thread 3: 1
Thread 1: 1
Thread 2: 1
Thread 4: 1
Thread 5: 0
Thread 5: done!
Thread 3: 0
Thread 3: done!
Thread 1: 0
Thread 1: done!
Thread 4: 0
Thread 4: done!
Thread 2: 0
Thread 2: done!
It should be evident that the threads are running asynchronously.
|