public class Example {
private int stateVal = 0;
public static void main(String[] args) {
Example a = new Example(50); // Line 4
}
public Example(Integer stateVal) {
this.stateVal = stateVal; // Line 7
Thread a = new MyThread("Thread-a");
Thread b = new MyThread("Thread-b"); // Line 9
a.start();
b.start();
while ((a.isAlive()== false ) || (b.isAlive()== false)) {
try {
this.wait(1000);
} catch ( InterruptedException e) { }
}
synchronized ( this) {
this.stateVal++;
}
}
private synchronized int getStateVal() {
return this.stateVal;
}
public class MyThread extends Thread {
int stateVal = 0;
public MyThread(String name) {
super(name);
}
public void run(){
for (;;) {
System.out.println(this.getName());
System.out.println(Example.this.getStateVal());
System.out.println(this.stateVal);
this.stateVal++;
}
}
}
}
所以,如果你要在每一行代表这个多线程程序的堆栈和堆
第7行
第9行
第4行
由此我想了解Java如何在堆中和堆栈中分配内存。 或许以某种方式表示这一点,表示参考/对象链接,它表示当控制流刚刚执行了指令时程序的状态。你会怎么代表它?图表可能有帮助吗?