在 Java 中有两种方式创建线程:
本节的示例代码在 com.elanzone.books.noteeg.chpt1.sect02 package中
线程类 : Calculator : implements Runnable
@Override public void run() { // 输出乘法结果 for (int i = 1; i <= ThreadNum; i++) { System.out.printf("%s: %d * %d = %d\n", Thread.currentThread().getName(), number, i, i * number); } }
控制类 : Main
public static void main(String[] args) { for (int i = 1; i <= 10; i++) { Calculator calculator = new Calculator(i); Thread thread = new Thread(calculator); thread.start(); } }