private int age;
private String name;
public int getAge() {
return age;
}
public Student() {
super();
System.out.println("线程 " + Thread.currentThread().getName() + " 初始化" + this.getClass().getName());
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public enum InstanceDemo {
INSTANCE,;
private Student student = new Student();
/*
* private InstanceDemo() { student = new Student(); }
*/
public Student getStudent() {
return student;
}
}
public class Demo2 {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread() {
public void run() {
Student student = InstanceDemo.INSTANCE.getStudent();
Student student2 = InstanceDemo.INSTANCE.getStudent();
System.out.println("线程: " + Thread.currentThread().getName() + " , student = " + student);
System.out.println("线程: " + Thread.currentThread().getName() + " , student2 = " + student2);
System.out.println("线程: " + Thread.currentThread().getName() + "== :" + (student == student2));
}
}.start();
}
}
}