修改线程的名字,添加线程的动作线程的动作

修改线程的名字是指:正在执行动作的线程的姓名。
添加线程的动作是指:执行动作的线程所做的工作。
如何给线程改名字

public class Test4 {

    public static void main(String[] args) {
        MyThread3 myThread3 = new MyThread3("搬砖的");
        myThread3.start();
//其实可以直接用myThread3.set.Name();改名字.
    }
}

class MyThread3 extends Thread{

    String type;

    public MyThread3(String name) {
        super(name);
        //this.name = name;
    }

//  public String getName() {
//      return name;
//  }
//
//
//
//  public void setName(String name) {
//      this.name = name;
//  }

//  Thread 中的  setName 和getName 是用final修饰的   所以子子类不能够重写此方法


    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(getName()+""+i);
            //  Thread-0
            // Thread-1
        }
    }
}

修改线程的名字(执行的人的姓名)
还有线程的动作(这个工人做的工作)

 /*
  *  知道  执行的人的姓名        这个工人做的工作
  *
  */
public class Test5 {

    public static void main(String[] args) {

        MyThread4 thread4 = new MyThread4("黄大明","搬砖");
        thread4.start();

        MyThread4 thread42 = new  MyThread4("baby", "在大明背后偷偷的数砖块");
        thread42.start();

        //......

    }
}

class MyThread4 extends Thread{

    String work;

    public MyThread4(String name,String work){
        super(name);
        this.work = work;
    }

    public String getWork() {
        return work;
    }

    public void setWork(String work) {
        this.work = work;
    }


    @Override
    public void run() {

        for (int i = 1; i < 11; i++) {
            System.out.println(getName()+"工作是"+work+"工作"+i+"次");
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值