package com.test;
public class TT implements Runnable {
int b = 100;
public synchronized void m1() throws Exception{
b = 1000;
Thread.sleep(5000);
System.out.println("b = " + b);
}
=======================================
public synchronized void m2() throws Exception {
Thread.sleep(2500);
b = 2000;
}
=======================================
public void run() {
try {
m1();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
TT tt = new TT();
Thread t = new Thread(tt);
t.start();
tt.m2();
}
}
=======================================
小弟认为结果应该是2000,理由:在m1执行后此时b值为1000且休眠5秒,但是m2将b值修改为2000且休眠2.5秒,等到m1的5秒结束后显示b值,此时b值为2000。
但是,系统出来的结果是1000,小弟不理解,请求指点。谢谢!