黑马程序员技术交流社区

标题: 小问题,求解 [打印本页]

作者: stormdzh    时间: 2013-9-9 09:02
标题: 小问题,求解
本帖最后由 杨增坤 于 2013-9-11 17:39 编辑

class TheredText
{
public static void main(String[] args)
{
  MyThread myThread=new MyThread();
  myThread.start();
}
}
class MyThread extends Thread
{
    MyThread(){};
int i=0;
boolean flag=true;
public void run()
{
   while(flag)
  {
       if(i<10000)
   {
     System.out.println("this is the input:" +i);
     i++;
      }
   else
    flag=false;
     }
}
}
输出结果为:
this is the input:9701
this is the input:9702
this is the input:9703
this is the input:9704
this is the input:9705
this is the input:9706
this is the input:9707
this is the input:9708
this is the input:9709
this is the input:9710
this is the input:9711
this is the input:9712
this is the input:9713
this is the input:9714
this is the input:9715
this is the input:9716
this is the input:9717
this is the input:9718
this is the input:9719
this is the input:9720
this is the input:9721
this is the input:9722
this is the input:9723
this is the input:9724
this is the input:9725
问题一:为什么它的输出是从9701开始?


作者: 肖勇    时间: 2013-9-9 09:23
这只是计算机的运行速度太快,控制台只能容下那么多数据,当他运行的时候你可以强行终止,然后看下结果。this is the input:1949this is the input:1950
this is the input:1951
this is the input:1952
this is the input:1953
this is the input:1954
this is the input:1955
this is the input:1956
this is the input:1957
this is the input:1958
this is the input:1959
this is the input:1960
this is the input:1961
this is the input:1962
this is the input:1963
this is the input:1964
this is the input:1965
this is the input:1966
this is the input:1967
this is the input:1968
this is the input:1969
this is the input:1970
this is the input:1971
this is the input:1972



作者: 毋须繁华    时间: 2013-9-9 09:51
cmd 命令提示符窗口的最大显示行数为300,你的这行代码运行的同时,一直在滚动,上面的被都覆盖了。在eclipse里可以看的更多,但也只有从6666开始的。建议把while 中的循环次数减少,可以看的更直观一点。
作者: 杨彬    时间: 2013-9-9 13:32
是因为执行速度太快了,你没看到而已,你可以sleep一下就行了
作者: 陶智第    时间: 2013-9-10 08:38
输出是从0开始的
可以通过control+C控制线程结束的时机




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2