20090219/ Java thread 整理
1. 如何 Create Java thread
A. 實作Runnable介面
宣告:
class A extends B implements Runnable
{
public A() {
}
public void run()
{ ……… }
}
啟動:
A th = new A();
Thread t1 = new Thread(th,"執行緒A");
t1.start(); //啟動的程式碼比下面多一排
B. 繼承Thread類別
宣告:
class A extends Thread
{
public A(int length, String name) {
}
public void run()
{ ………
}
}
啟動:
A th = new A();
th.start();
2. 同一支Java file 如何產生兩個不同的 thread code
class ThreadTest1 {
public static void main(String[] args){
MyThread T = new MyThread();
MyRunnable R = new MyRunnable();
Thread TR = new Thread(R);
T.start();
TR.start();
}
}
class MyThread extends Thread {
public void run(){
System.out.println("MyThread run...");
}
}
class MyRunnable implements Runnable {
public void run(){
System.out.println("MyRunnable run...");
}
}
參考自
http://www.javaworld.com.tw/jute/post/view?bid=29&id=102834&sty=3
0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁