|
|
Doing Two or More Tasks At Once: Threads |
Thread and Overriding run
The first way to customize what a thread does when it is running is to subclassThread(itself aRunnableobject) and override its emptyrunmethod so that it does something. Let's look at theSimpleThreadclass, the first of two classes in this example, which does just that:The first method in thepublic class SimpleThread extends Thread { public SimpleThread(String str) { super(str); } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i + " " + getName()); try { sleep((int)(Math.random() * 1000)); } catch (InterruptedException e) {} } System.out.println("DONE! " + getName()); } }SimpleThreadclass is a constructor that takes aStringas its only argument. This constructor is implemented by calling a superclass constructor and is interesting to us only because it sets theThread's name, which is used later in the program.The next method in the
SimpleThreadclass is therunmethod. Therunmethod is the heart of anyThreadand where the action of theThreadtakes place. Therunmethod of theSimpleThreadclass contains aforloop that iterates ten times. In each iteration the method displays the iteration number and the name of theThread, then sleeps for a random interval of up to 1 second. After the loop has finished, therunmethod printsDONE!along with the name of the thread. That's it for theSimpleThreadclass.The
TwoThreadsTestclass provides amainmethod that creates twoSimpleThreadthreads: one is named "Jamaica" and the other is named "Fiji". (If you can't decide on where to go for vacation you can use this program to help you decide--go to the island whose thread prints "DONE!" first.)Thepublic class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji").start(); } }mainmethod also starts each thread immediately following its construction by calling thestartmethod. To save you from typing in this program, click here for the source code to theSimpleThreadclass and here for the source code to theTwoThreadsTestprogram. Compile and run the program and watch your vacation fate unfold. You should see output similar to the following:(Looks like I'm going to Fiji!!) Notice how the output from each thread is intermingled with the output from the other. This is because both0 Jamaica 0 Fiji 1 Fiji 1 Jamaica 2 Jamaica 2 Fiji 3 Fiji 3 Jamaica 4 Jamaica 4 Fiji 5 Jamaica 5 Fiji 6 Fiji 6 Jamaica 7 Jamaica 7 Fiji 8 Fiji 9 Fiji 8 Jamaica DONE! Fiji 9 Jamaica DONE! JamaicaSimpleThreadthreads are running concurrently. Thus, bothrunmethods are running at the same time and each thread is displaying its output at the same time as the other.
Try This: Change the main program so that it creates a third thread with the name "Bora Bora". Compile and run the program again. Does this change the island of choice for your vacation? Here's the code for the new main program, which is now namedThreeThreadsTest.
Now, let's look at another example, the
Clockapplet, that uses the other technique for providing arunmethod to aThread.
|
|
Doing Two or More Tasks At Once: Threads |