本帖最后由 要你快乐 于 2014-5-18 19:15 编辑
- public class BianLiangDemo implements Runnable{
- int b=100;
- public synchronized void m1()throws Exception{
- //Thread.sleep(5000);
- b=1000;
- System.out.println("m1 b="+b);
- }
- public synchronized void m2() throws Exception{
- Thread.sleep(2500);
- b=2000;
- System.out.println("m2 b="+b);
- }
- public void run(){
- try{
- m1();
- }catch(Exception e){e.printStackTrace();}
- }
- public static void main(String[] args)throws Exception {
- BianLiangDemo t=new BianLiangDemo();
- Thread t1=new Thread(t);
- t1.start();
- t.m2();
- System.out.println("t="+t.b);
- }
- }
复制代码
为什么输出结果是:m2 b=2000t=2000
m1 b=1000
而不是:m1 b=1000
m2 b=2000t=2000呢?
|
|