50,545
社区成员
发帖
与我相关
我的任务
分享
class InheritThread extends Thread{
private int ticketNum=10;
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
for(int i=0;i<20;i++){
if(this.ticketNum>0)
System.out.println(“Thread "+this.ticketNum--);
//如何在这里取得对象的名字
}
}
}
public class TestThread {
public static void main(String[] args) {
// TODO Auto-generated method stub
InheritThread mt1=new InheritThread();
InheritThread mt2=new InheritThread();
InheritThread mt3=new InheritThread();
mt1.start();
mt2.start();
mt3.start();
}
}
private int ticketNum=10;
private String name;
public InheritThread(String name){
this.name=name;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
for(int i=0;i<20;i++){
if(this.ticketNum>0){
System.out.println("Thread"+this.ticketNum--);
System.out.println("Thread"+this.name);
}
//如何在这里取得对象的名字
}
}
InheritThread mt1=new InheritThread("name1");
InheritThread mt2=new InheritThread("name2");
InheritThread mt3=new InheritThread("name3");
mt1.start();
mt2.start();
mt3.start();