实现多线程的两种方法.
/**继承Thread类*/
public class Test extends Thread {
public Test() {
Thread t = new Thread(this);
t.start();
}
public static void main(String[] args) {
Test t = new Test();
}
public void run(){
while(true)
System.out.println("继承Thread类");
}
}
/**实现Runnable接口*/
public class Test2 implements Runnable {
public Test2(){
Thread t = new Thread(this);
t.start();
}
public static void main(String[] args) {
Test2 t2 = new Test2();
}
public void run() {
while(true)
System.out.println("实现Runnable接口");
}
时间: 2024-10-24 08:20:10