//多執行緒範例 import java.io.*; class MT extends Thread //繼承Thread類別 { private String msg; public MT(String inp) { msg=inp; } public MT() { this("Thread Test!"); } public void run()//執行緒真正跑的部分 { int i,j; try { for (i=0;i<10;i++) { sleep((int) (Math.random()*1000));//停止亂數時間 System.out.println(msg+"嗆聲!"); } } catch (InterruptedException e) { } } } public class demo32{ //Java 是由類別組成的 public static void main(String args[]) { //程式進入點 MT test1=new MT("第一號!"); MT test2=new MT("第十號!"); test1.start();//啟動執行緒 test2.start();//啟動執行緒 } }