1、想了解不同类型线程之间的关系(此程序没考虑线程安全问题),想模拟电脑是怎样执行多线程,但是得到一个莫名奇妙的结果。思考了很长时间,还是没有解决,希望能得到帮助;
2、代码如下
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、得到的结果为:
null----27
请问是怎样得到这样的结果的?
扬帆大鱼
至尊宝的传说
慕运维8079593
FFIVE
相关分类