本帖最后由 贾存双 于 2012-7-12 20:31 编辑
class MyThread implements Runnable{
public void run(){
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName() + "运行:i=" + i) ;
}
}
} ;
public class RunnableDemo{
public static void main(String args[]){
MyThread thr1 = new MyThread() ;
MyThread thr2 = new MyThread() ;
Thread a = new Thread(thr1,"线程A") ; //3:这里可以证明Thread类中有带两个参数的构造方法
Thread b = new Thread(thr2,"线程B") ;
a.start() ;
b.start() ;
}
} ;
=============================================================
class MyThread extends Thread{
public MyThread(String name){ //2:为什么继承Thread类,要写一个setName()的构造构造方法,而上面那个继承Runnable接口却不用写???
super(name) ; //1:既然写super(name)会起作用,那么Thread类中应该也会有带一个参数的构造方法啊!
}
public void run(){
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName() + "运行,i=" +i ) ;
}
}
} ;
public class ThreadDemo{
public static void main(String args[]){
MyThread thr1 = new MyThread("线程A") ;
MyThread thr2 = new MyThread("线程B") ;
thr1.start() ;
thr2.start() ;
}
} ; |
|