1.public?class?DieThreadDemo?{
public?static?void?main(String[]?args)?{
Example?example=new?Example();
helloThread1?thread1=new?helloThread1(example);
thread1.start();
helloThread2?thread2=new?helloThread2(example);
thread2.start();
}
}
class?helloThread1?extends?Thread{
private?Example?example=null;
public?helloThread1(Example?example){
this.example=example;
}
@Override
public?void?run()?{
example.method1();
}
}
2.public?class?hello?{
public?static?void?main(String[]?args)?{
helloThread1?thread1=new?helloThread1();
thread1.start();
helloThread2?thread2=new?helloThread2();
thread2.start();
}
}
class?helloThread1?extends?Thread{
Example?example=new?Example();
public?void?run(){
example.method1();
}
}
1 回答

風(fēng)一般的男人
TA貢獻(xiàn)6條經(jīng)驗 獲得超3個贊
java里面是new Thread().start(),就是都是先實例化類,在調(diào)用run()方法,然后再通過run()方法調(diào)用其他方法。
不同之處在于,第一種可以向子線程傳值,而第二種不能傳值,因為第二種線程的構(gòu)造方法只有空的構(gòu)造方法。
添加回答
舉報
0/150
提交
取消