⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testthread.java

📁 此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码。
💻 JAVA
字号:
// TestThread.java: Define threads using the Thread class
public class TestThread
{
  // Main method
  public static void main(String[] args)
  { 
    // Create threads
    PrintChar printA = new PrintChar('a',100);
    PrintChar printB = new PrintChar('b',100);
    PrintNum  print100 = new PrintNum(100);

    // Start threads
    print100.start();
    printA.start();
    printB.start();
  }
}
/*
// The thread class for printing a specified character
// in specified times
class PrintChar extends Thread
{
  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=1; i < times; i++)
      System.out.print(charToPrint);
  }
}

// The thread class for printing number from 1 to n for a given n
class PrintNum extends Thread
{
  private int lastNum;

  // Construct a thread for print 1, 2, ... i
  public PrintNum(int n)
  {
    lastNum = n;
  }

  public void run()
  {
    for (int i=1; i <= lastNum; i++)
      System.out.print(" " + i);
  }
} */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -