修改线程的名字是指:正在执行动作的线程的姓名。
添加线程的动作是指:执行动作的线程所做的工作。
如何给线程改名字
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+"次");
}
}
}