|
|
|
|
There are, however, situations where you want to have a class do thread work, but you also need that class to extend from another class. Since Java doesn't allow you to extend from more than a single class, you wouldn't be able to use the above technique. In this case, you'll need to use the Runnable interface instead. In general, you may prefer to use the Runnable technique, because it's more flexible. Here's how you do the same thing as above, but using the Runnable interface:
and here's the corresponding test program:
We still use the Thread class to cause a thread to start, but now we use it directly, specifying in the Thread constructor the class that implements the Runnable interface, to associate the runnable class with the thread. The effect of using the Runnable interface is to separate the work (the code in the run() method) from the context in which it runs (the thread). Here's what this program produced on my machine when I ran it (note that the actual output may vary because of the nature of multi-threading applications): Thread 1: started (counting from 6) Thread 2: started (counting from 7) Thread 3: started (counting from 9) Thread 4: started (counting from 6) Thread 5: started (counting from 9) Thread 1: 6 Thread 2: 7 Thread 3: 9 Thread 4: 6 Thread 5: 9 Thread 1: 5 Thread 2: 6 Thread 3: 8 Thread 4: 5 Thread 5: 8 Thread 2: 5 Thread 1: 4 Thread 5: 7 Thread 4: 4 Thread 3: 7 Thread 2: 4 Thread 1: 3 Thread 5: 6 Thread 3: 6 Thread 4: 3 Thread 2: 3 Thread 1: 2 Thread 5: 5 Thread 4: 2 Thread 3: 5 Thread 2: 2 Thread 1: 1 Thread 5: 4 Thread 3: 4 Thread 4: 1 Thread 2: 1 Thread 1: 0 Thread 1: done! Thread 5: 3 Thread 4: 0 Thread 4: done! Thread 3: 3 Thread 2: 0 Thread 2: done! Thread 5: 2 Thread 3: 2 Thread 5: 1 Thread 3: 1 Thread 5: 0 Thread 5: done! Thread 3: 0 Thread 3: done!
|
| The page was last updated February 19, 2008 |