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

我想飞走 2012-04-04 10:22:29

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();

}

}

...全文
47 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
我想飞走 2012-04-04
  • 打赏
  • 举报
回复
谢谢楼上的,知道少些了一个
我想飞走 2012-04-04
  • 打赏
  • 举报
回复
打印结果是

B2 = 2000


明显是没调用F1的方法,但是我在RUN()里面写了调用F1啊
sdojqy1122 2012-04-04
  • 打赏
  • 举报
回复
TestSync2 ts = new TestSync2();
Thread tst = new Thread(ts);
tst.start();
f2();
....

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧