2. 程式一1. THREAD()
public class ThreadExample1 extends Thread {
public void run() { // override Thread's run()
System.out.println("Here is the starting point of Thread.");
for (;;) { // infinite loop to print message
System.out.println("User Created Thread");
}
}
public static void main(String[] argv) {
Thread t = new ThreadExample1(); // 產生Thread物件
t.start(); // 開始執行t.run()
for (;;) {
System.out.println("Main Thread");
}
}
}
3.
4. 程式二2. THREAD(RUNNABLE)
public class ThreadExample2 implements Runnable {
public void run() { // implements Runnable run()
System.out.println("Here is the starting point of Thread.");
for (;;) { // infinite loop to print message
System.out.println("User Created Thread");
}
}
public static void main(String[] argv) {
Thread t = new Thread(new ThreadExample2()); // 產生Thread
物件
t.start(); // 開始執行Runnable.run();
for (;;) {
System.out.println("Main Thread");
}
}
}