[求助]这个程序的结果为什么是这样?
该程序的结果为什么会是
2000
1000
1000
我的理解是这样的!不管线程1还是线程2优先执行,是不是主线程都能优先输出System.out.println(tt.b);
这样应该能看到100的值。但是为什么每次都是这个结果?sleep不是不释放锁吗?谁可以给个详细的解释?谢谢!
public class TT implements Runnable{ int b = 100; public synchronized void run(){ try { modify_1(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public synchronized void modify_1() throws InterruptedException{ b=1000; Thread.sleep(5000); System.out.println(b); } public synchronized void modify_2() throws InterruptedException{ Thread.sleep(2500); b=2000; System.out.println(b); } public static void main(String[] args) throws InterruptedException { TT tt = new TT(); Thread th = new Thread(tt); th.start(); tt.modify_2(); System.out.println(tt.b); }}