读书人

这个程序为什么只执行了一步打印了

发布时间: 2012-04-05 12:42:40 作者: rapoo

求助,这个程序为什么只执行了一步,打印了第二个方法,第一个是没执行还是没打印。

Java code
public class TestSync2 implements Runnable{    int b = 0;        public synchronized void f1(){        try{        Thread.sleep(1000);    } catch (InterruptedException ie){}        int b = 1000;        System.out.println("B1 = " + b);                }            public static void f2(){        try{        Thread.sleep(500);    }    catch (InterruptedException ie){}        int b = 2000;        System.out.println("B2 = " + b);                }            public void run(){            f1();        }                    public static void main(String[] args){        TestSync2 ts = new TestSync2();        Thread tst = new Thread();        tst.start();        f2();                }    }


[解决办法]
TestSync2 ts = new TestSync2();
Thread tst = new Thread(ts);
tst.start();
f2();
....

读书人网 >J2SE开发

热点推荐