1. Want to understand the relationship between different types of threads (this program does not consider thread safety), want to simulate how the computer executes multithreading, but get an inexplicable result. After thinking about it for a long time, I still haven"t solved it, hoping to get help.
2, the code is as follows
public class StudentDemo {
public static void main(String[] args) {
Student s = new Student();
SetThread st = new SetThread(s);
GetThread gt = new GetThread(s);
Thread t1 = new Thread(st, "SetTread");
Thread t2 = new Thread(gt,"GetTread");
t2.start();
t1.start();
}
}
public class Student {
public String name;
public int age;
}
public class SetThread implements Runnable{
private Student s;
public SetThread(Student s) {
this.s = s;
}
@Override
public void run() {
s.name = "jzian";
s.age = 27;
}
}
public class GetThread implements Runnable {
private Student s;
public GetThread(Student s) {
this.s = s;
}
@Override
public void run() {
System.out.println( s.name + "-----" + s.age);
}
}
3. The result is:
null-27
how did you get such a result?