废话不多说,直接上代码
show code
/**
* @Auther: 罗罗
*/
public class Test05 {
public static void main(String[] args) throws InterruptedException {
Student student = new Student();
new T1(student).start();
new T2(student).start();
}
}
class T1 extends Thread{
private Student student;
public T1(Student student){
this.student = student;
}
@Override
public void run() {
student.sell("a");
}
}
class T2 extends Thread{
private Student student;
public T2(Student student){
this.student = student;
}
@Override
public void run() {
student.sell("b");
}
}
class Student{
private int num = 0;
public void sell(String name){
if (name.equals("a")){
num = 100;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
num = 200;
}
System.out.println(name+" num = "+num);
}
}
"C:\Program Files\Java\jdk1.8.0_144\bin\java.exe" "-javaagent:D:\IntelliJ IDEA\IntelliJ IDEA 2018.2.4\lib\idea_rt.jar=56700:D:\IntelliJ IDEA\IntelliJ IDEA 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;C:\Program "
b num = 200
a num = 200
Process finished with exit code 0
我们试试把变量移到方法内,看看运行的效果,多测几次
public class Test05 {
public static void main(String[] args) throws InterruptedException {
Student student = new Student();
new T1(student).start();
new T2(student).start();
}
}
class T1 extends Thread{
private Student student;
public T1(Student student){
this.student = student;
}
@Override
public void run() {
student.sell("a");
}
}
class T2 extends Thread{
private Student student;
public T2(Student student){
this.student = student;
}
@Override
public void run() {
student.sell("b");
}
}
class Student{
public void sell(String name){
int num = 0;
if (name.equals("a")){
num = 100;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
num = 200;
}
System.out.println(name+" num = "+num);
}
}
"C:\Program Files\Java\jdk1.8.0_144\bin\java.exe" "-javaagent:D:\IntelliJ IDEA\IntelliJ IDEA Files\Java\jdk1.8.0_144\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;C:\Program "
b num = 200
a num = 100
Process finished with exit code 0
为什么会这样呢?还记得前面我写的 罗罗初探JVM 吗,看过的小伙伴就知道为什么会出现上面的情况 ,感兴趣的小伙伴可以去回顾一下,下面我把关于这篇文章的需要用到的提取出来。
栈帧中主要保存3类数据:
1.本地变量(Local Variables) :输入参数和输出参数以及方法内的变量;
2.栈操作(Operand Stack) :记录出栈、入栈的操作;
3.栈帧数据(Frame Data) :包括类文件、方法等等。
线程执行一个方法就会被压入栈,而栈是每一个线程私有,所以方内的变量是线程私有的,既然是私有,肯定是安全的啊,哈哈,是不是突然就通了。