A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 隋玉翠 黑马帝   /  2011-8-9 21:15  /  1571 人查看  /  0 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

问题:为什么输出的都为1000?

public class TT implements Runnable {
        int b = 100;
       
        public synchronized void m1() throws Exception{
                //Thread.sleep(2000);
                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();
                System.out.println(tt.b);
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马