關乎Java多執行緒+Runnable和Thread…
兩種方法
一種是繼承自Thread類.Thread 類是一個具體的類,即不是抽象類,該類封裝了執行緒的行為.要建立一個執行緒,程式設計師必須建立一 個從 Thread 類匯出的新類.程式設計師通過覆蓋 Thread 的 run() 函式來完成有用的工作.使用者並不直接呼叫此函式;而是通過調 用 Thread 的 start() 函式,該函式再呼叫 run().
例如:
public class Test extends Thread{
public Test(){
}
public static void main(String args[]){
Test t1 = new Test();
Test t2 = new Test();
t1.start();
t2.start();
}
public void run(){
//do thread's things
}
}
--------------------------------------------------------------------------------
另一種是實現Runnable介面,此介面只有一個函式,run(),此函式必須由實現了此介面的類實現.
例如:
public class Test implements Runnable{
Thread thread1;
Thread thread2;
public Test(){
thread1 = new Thread(this,"1");
thread2 = new Thread(this,"2");
}
public static void main(String args[]){
Test t = new Test();
t.startThreads();
}
public void run(){