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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 聖手`书生 中级黑马   /  2013-4-10 22:20  /  1312 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Ticket implements Runnable//extends Thread
{
        private  int tick = 100;
        public void run()
        {
                while(true)//真是阴沟里翻船,不明白true判断的是哪里的内容,求指点。
                {
                        if(tick>0)
                        {
                                System.out.println(Thread.currentThread().getName()+"....sale : "+ tick--);
                        }
                }
        }
}


class  TicketDemo
{
        public static void main(String[] args)
        {

                Ticket t = new Ticket();

                Thread t1 = new Thread(t);
                Thread t2 = new Thread(t);
                Thread t3 = new Thread(t);
                Thread t4 = new Thread(t);
                t1.start();
                t2.start();
                t3.start();
                t4.start();

        }
}

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

10 个回复

倒序浏览
是tick的值
回复 使用道具 举报
从while(true)开始循环,也就是说当程序执行到 while(true){ } 时循环条件永远为真,也就是所说的无限循环,这样用时必须在循环体重定义退出循环的语句,否则程序将陷入死循环,也就是里面的if(tick>0)判断条件,当条件不满足时跳出循环。

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 通行天下 于 2013-4-10 22:37 编辑
  1. 我改了一下,现在可以啦;希望对你有帮助。

  2. class Ticket implements Runnable//extends Thread
  3. {
  4.          private  int tick = 100;
  5.          public void run()
  6.          {
  7.                  while(true)//在这里true只是while循环的一个判断依据而已
  8.                  {
  9.                          if(tick>0)
  10.                          {
  11.                                  System.out.println(Thread.currentThread().getName()+"....sale : "+ tick--);
  12.                          }
  13.                         //当ticket=0时,就跳出循环,结束运行;不然程序就一直会处于等待中,不会结束。
  14.                          else
  15.                                break;
  16.                  }
  17.          }
  18. }


  19. class  TicketDemo
  20. {
  21.          public static void main(String[] args)
  22.         {

  23.                 Ticket t = new Ticket();

  24.                 Thread t1 = new Thread(t);
  25.                  Thread t2 = new Thread(t);
  26.                  Thread t3 = new Thread(t);
  27.                  Thread t4 = new Thread(t);
  28.                  t1.start();
  29.                  t2.start();
  30.                  t3.start();
  31.                  t4.start();

  32.         }
  33. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
while(true) 判断 if(tick>0)

tick--,当0<tick<100--->true,返回while(true)中继续判断;
当tick<0------>falsel,跳出while语句

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
此程序如果不按ctrl + c 的话是不会停下来,因为 有while(true)是让线程一直运行,你while里没有结束循环的标志。
回复 使用道具 举报
while(条件语句){

}
只要条件为true就执行循还,而此例直接给好了true,虚拟机就继续执行while循还,对于根据什么作判断,也当然就是true本身了,为true则继续循还,为false就终止循还.这是原理,while循还的规定.
回复 使用道具 举报
while(true)死循环,在if语句后加一个 else 语句,里面break;跳出循环。
回复 使用道具 举报
貌似会陷入死循环呀!
回复 使用道具 举报
While(boolean){}
while循环中接收的是一个boolean参数,这个参数为真,循环体就开始循环,为假就跳出循环了。
while(true)//真是阴沟里翻船,不明白true判断的是哪里的内容,求指点。
                {
                        if(tick>0)
                        {
                                System.out.println(Thread.currentThread().getName()+"....sale : "+ tick--);
                        }
                }
在你的这个程序里,true判断的就是这个while后面的{}里的代码。因为条件是true while语句就会无限的循环这里面的代码 即if(tick>0)
                        {
                                System.out.println(Thread.currentThread().getName()+"....sale : "+ tick--);
                        }

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
楼上回答的应该可以帮你了。
while(true){ }就是死循环。你需要用break;跳出循环
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马