多线程的一点小问题!!
(1)(2)这两个地方写成 Person p = new Person() ;和Student s = new Person() ;就出Exception in thread "main" java.lang.StackOverflowError,为什么??
public class Runnable_Test {
public static void main(String[] arg) {
Person p1 = new Person() ;
Student s1 = new Student() ;
Thread thread = new Thread(p1) ;
thread.start() ;
Thread thread1 = new Thread(s1) ;
thread1.start() ;
}
}
class Person implements Runnable {
private String names ;
Person p = null ; //**************************(1)
public void run() {
p = new Person();
for(int i=0; i<100; i++) {
p.setNames("Person" + i) ;
System.out.println("PersonName---->" + p.getNames()) ;
}
}
public void setNames(String names) {
this.names = names ;
}
public String getNames() {
return this.names ;
}
}
class Student extends Person implements Runnable {
private int age ;
Student s = null ; //**************************************(2)
public void run() {
s = new Student() ;
for(int i=0; i<100; i++) {
s.setNames("Student:" + i) ;
s.setAge(i) ;
System.out.println("StudentName~~~~" + s.getNames());
System.out.println("StudentAge*******" + s.getAge());
}
}
public void setAge(int age) {
this.age = age ;
}
public int getAge() {
return this.age ;
}
}