package chapter15; /** * Title: Chapter 15, "Multithreading" * Description: Examples for Chapter 15 * Copyright: Copyright (c) 2000 * Company: Armstrong Atlantic State University * @author Y. Daniel Liang * @version 1.0 */ // TestRunnable.java: Define threads using the Runnable interface public class TestRunnable { /**Main method*/ public static void main(String[] args) { new TestRunnable(); } /**Default constructor*/ public TestRunnable() { // Create threads Thread printA = new Thread(new PrintChar('a', 100)); Thread printB = new Thread(new PrintChar('b', 100)); Thread print100 = new Thread(new PrintNum(100)); // Start threads print100.start(); printA.start(); printB.start(); } // The thread class for printing a specified character // in specified times class PrintChar implements Runnable { private char charToPrint; // The character to print private int times; // The times to repeat /**Construct a thread with specified character and number of times to print the character */ public PrintChar(char c, int t) { charToPrint = c; times = t; } /**Override the run() method to tell the system what the thread will do */ public void run() { for (int i=0; i<=times; i++) System.out.print(charToPrint); } } // The thread class for printing number from 1 to n for a given n class PrintNum implements Runnable { private int lastNum; /**Construct a thread for print 1, 2, ... i*/ public PrintNum(int n) { lastNum = n; } /**Tell the thread how to run*/ public void run() { for (int i=1; i <= lastNum; i++) System.out.print(" " + i); } } }