黑马程序员技术交流社区
标题:
关于Thread类中的构造方法
[打印本页]
作者:
贾存双
时间:
2012-7-12 18:48
标题:
关于Thread类中的构造方法
本帖最后由 贾存双 于 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() ;
}
} ;
作者:
苑占丽
时间:
2012-7-12 18:58
class MyThread extends Thread{
public MyThread(String name){ //2:无什么继承Thread类,要写一个setName()的构造构造方法,而上面那
个继承Runnable接口却不用写???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() ;
}
} ;
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() ;
}
} ;
class MyThread implements Runnable{这是实现不是继承,借口只能实现它呀,不能继承的。
作者:
苑占丽
时间:
2012-7-12 19:00
public MyThread(String name){ //2:无什么继承Thread类,要写一个setName()的构造构造方法,
而上面那个继承Runnable接口却不用写???
因为上面的不是继承,而是实现借口
作者:
游兴钟
时间:
2012-7-12 19:00
Runnable接口和Thread类又没有直接的关系 如果非要说有关系
那也就是Thread(new MyThread())时作为参数传递进来的时候才有关系
这个接口很简单 单纯就是提供一个可以存放多线程运行代码的地方 里面就一个抽象的run方法
所以根本没有必要往里面传什么参数
作者:
陈淑飞
时间:
2012-7-12 19:08
楼主不要纠结这个name 参数了。
super(name),Thread中肯定有一个String参数的,构造方法。
为什么有要name。只是为了自定义一个线程名称而已,若不传入的话, 似乎系统会默认成 Thread-x 之类的。
用按口实现时,也是通过Thread构造方法中,传入一个name呀。
Thread a = new Thread(thr1,"线程A") ; //3:这里可以证明Thread类中有带两个参数的构造方法
作者:
柯玲
时间:
2012-7-13 11:06
可以查看jdk中Thread类的构造方法摘要:
自定义线程名称有两种方法:(1)形如public MyThread(String name){ super(name) ; }(2)子类重写setName()方法设定线程名称;
如果不用这两种方法,就会得到系统默认的线程名称:Thread-编号;
其中用super(name)是因为MyThread的爸爸Thread已经描述了私有的变量name,并完成了通过get、set方法进行访问的操作。子类只需要引用就可以了。
声明一点:子类通过构造方法设置的名称是不能替换系统默认线程名称的。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2