26.7. Key Terms

 
[Page 774 ( continued )]

24.3. Creating Tasks and Threads

Tasks are objects. To create tasks, you have to first declare a class for tasks. A task class must implement the Runnable interface. The Runnable interface is rather simple. All it contains is the run method. You need to implement this method to tell the system how your thread is going to run. A template for developing a task class is shown in Figure 24.2(a).

Figure 24.2. Define a task class by implementing the Runnable interface.
(This item is displayed on page 775 in the print version)

Once you have declared a TaskClass , you can create a task using its constructor. For example,

 TaskClass task =   new   TaskClass(...); 


[Page 775]

A task must be executed in a thread. The Thread class contains the constructors for creating threads and many useful methods for controlling threads. To create a thread for a task, use

 Thread thread =   new   Thread(task); 

You can then invoke the start() method to tell the JVM that the thread is ready to run, as follows :

 thread.start(); 

The JVM will execute the task by invoking the task's run() method. Figure 24.2(b) outlines the major steps for creating a task, a thread, and start the thread.

Listing 24.1 gives a program that creates three tasks and three threads to run them:

  • The first task prints the letter a one hundred times.

  • The second task prints the letter b one hundred times.

  • The third task prints the integers 1 through 100 .

If you run this program on a multiple-CPU system, all three threads will execute simultaneously . If you run the program on a single-CPU system, the three threads will share the CPU and take turns printing letters and numbers on the console. This is known as time-sharing . Figure 24.3 shows a sample run of the program.

Figure 24.3. Tasks printA , printB , and print100 are executed simultaneously to display the letter a one hundred times, the letter b one hundred times, and the numbers from 1 to 100.


[Page 776]
Listing 24.1. TaskThreadDemo.java
 1   public class   TaskThreadDemo {  2   public static void   main(String[] args) {  3  // Create tasks  4  Runnable printA =   new   PrintChar(   'a'   ,   100   );  5      Runnable printB =   new   PrintChar(   'b'   ,   100   );  6      Runnable print100 =   new   PrintNum(   100   );  7  8  // Create threads  9  Thread thread1 =   new   Thread(printA);  10      Thread thread2 =   new   Thread(printB); 11      Thread thread3 =   new   Thread(print100); 12 13  // Start threads  14  thread1.start();  15      thread2.start(); 16      thread3.start(); 17    } 18  } 19 20  // The task for printing a specified character in specified times  21  class PrintChar   implements   Runnable  { 22   private char   charToPrint;  // The character to print  23   private int   times;  // The times to repeat  24 25  /** Construct a task with specified character and number of  26  * times to print the character  27  */  28   public   PrintChar(   char   c,   int   t) { 29      charToPrint = c; 30      times = t; 31    } 32 33  /** Override the run() method to tell the system  34  * what the task to perform  35  */  36    public void   run()  { 37   for   (   int   i =     ; i < times; i++) { 38         System.out.print(charToPrint); 39       } 40     } 41  } 42 43  // The task class for printing number from 1 to n for a given n  44  class PrintNum   implements   Runnable  { 45   private int   lastNum; 46 47  /** Construct a task for printing 1, 2, ... i */  48   public   PrintNum(   int   n) { 49      lastNum = n; 50    } 51 52  /** Tell the thread how to run */  53    public void   run()  { 54   for   (   int   i =   1   ; i <= lastNum; i++) { 55        System.out.print(   " "   + i); 56      } 57    } 58  } 


[Page 777]

The program creates three tasks (lines 4 “6). To run them concurrently, three threads are created (lines 9 “11). The start() method (lines 14 “16) is invoked to start a thread that causes the run() method in the task to be executed. When the run() method completes, the thread terminates.

Because the first two tasks, printA and printB , have similar functionality, they can be defined in one task class PrintChar (lines 21 “41). The PrintChar class implements Runnable and overrides the run() method (lines 36 “40) with the print-character action. This class provides a framework for printing any single character a given number of times. The runnable objects printA and printB are instances of the PrintChar class.

The PrintNum class (lines 44 “58) implements Runnable and overrides the run() method (lines 53 “57) with the print-number action. This class provides a framework for printing numbers from 1 to n , for any integer n . The runnable object print100 is an instance of the class printNum class.

Important Note

The run() method in a task specifies how to perform the task. This method is automatically invoked by the JVM. You should not invoke it. Invoking run() directly merely executes this method in the same thread and no new thread is started.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net