s是您創建的新線程。而您的學生實例是s.target
當您運行線程構造函數創建s時,您注入了新的Runnable(學生實例在您的案例中)。
Thread s = new Thread(human,new Student(ca));
s是thread-x,Student是thread-y。它們是獨立的實例。
s.target是您創建的新Runnable學生。
希望這會有所幫助。
如果你想擁有相同的線程組,你必須將「Humans」ThreadGroup傳遞給Student線程。試試這個:
public class ThreadGroups {
public static void main(String[] args){
ThreadGroup human = new ThreadGroup("Humans");
Thread s1 = new Student(human, "studentThread");
Thread s = new Thread(human, s1);
System.out.println(s.getThreadGroup().getName());
System.out.println(s1.getThreadGroup().getName());
s.start();
}
static class Student extends Thread {
@Override
public void run() {
System.out.println(this.getThreadGroup().getName());
}
public Student(ThreadGroup group, String name) {
super(group, name);
}
public Student() {
}
}
}
有一個答案對你有幫助嗎? – Lothar