为什么布尔变量会阻止我的 else 语句打印?

Why does a Boolean variable prevent my else statement from printing?

所以我有一个计时器,每隔几秒创建一个对象并将它们放入队列中。我是 运行 一个 while 循环,如果队列为空或有人在其中,它会打印出来。

while(1>0){

        if(wait_list.isEmpty()){

            System.out.println("Waiting");


        }else{

            System.out.println("Good");

        }
    }
}

这很好用,它打印出 "waiting",直到有东西被放入队列,然后它打印出 "good"。第二个我尝试让它只打印一次,然后它永远不会打印出 else 语句。它只打印一次 "waiting" 然后什么也不做,即使一个对象被添加到队列中。

 boolean wait = true;

 while(1>0){

        if(wait_list.isEmpty()){
            if(wait == true){
            System.out.println("Waiting");
            wait = false;
            }
        }else{

            System.out.println("Good");



        }
    }

我是不是漏掉了一些非常简单的东西。无限循环或其他东西(除了 while 循环)。

听起来您可能想要更像这样的东西:

while(wait_list.isEmpty()){
   System.out.println("Waiting");
}
System.out.println("Good");

您的 while 循环将继续,直到 wait_list.isEmpty() returns 为假。然后在 while 循环结束后打印一次 'Good'。

您没有正确测试您的条件。看看关闭等待后会发生什么...

     if(wait_list.isEmpty()){ //still true. Goes in
        if(wait == true){ //false. Skips this
        System.out.println("Waiting");
        wait = false;
        }
      // nothing to do
    }else{

        System.out.println("Good");



    }

您永远不会重置布尔标志的值,这就是它只打印一次的原因。

在您的 else 块中将 wait 设置为 false 应该会重置布尔标志并允许您的 while 循环再次打印等待。

添加if(wait == true)条件不会影响外部if(wait_list.isEmpty())条件的执行。

您不得向队列中添加任何内容。确保 'timer' 肯定 添加对象到 wait_list。它需要一个单独的线程才能实现。

在您的 "Good" 块中,您需要实际使用队列中的一个值,并且需要重置 wait 标志。

下面是一个完全 运行 可用的示例。队列的内容无关紧要,所以我只是让它包含执行时间(计划的和实际的)。例如,我不想让它永远 运行,所以永无止境的 while 循环被更改为在 10 个值后停止。为了表明足够快地添加的多个值将在不打印的情况下得到处理 "Waiting",计时器将在大约 33% 的时间内添加 2 个值。

// Create queue, and task for adding values to queue
final ConcurrentLinkedQueue<Date[]> wait_list = new ConcurrentLinkedQueue<Date[]>();
TimerTask task = new TimerTask() {
    @Override public void run() {
        Date actualTime = new Date();
        Date scheduledTime = new Date(this.scheduledExecutionTime());
        wait_list.add(new Date[] { scheduledTime, actualTime });
        if (actualTime.getTime() % 3 == 0) // Add two elements about 33% of the time
            wait_list.add(new Date[] { scheduledTime, actualTime });
    }
};

// Run task every 0.1 second
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, 100, 100);

// Process next 10 values, printing "Waiting" whenever queue goes empty
SimpleDateFormat fmt = new SimpleDateFormat("mm:ss.SSS");
boolean waiting = false;
int countDown = 10;
while (countDown > 0) {
    if (wait_list.isEmpty()) {
        if (! waiting) {
            System.out.println("Waiting");
            waiting = true;
        }
    } else {
        Date[] date = wait_list.remove();
        System.out.println("Good: scheduled=" + fmt.format(date[0]) + ", actual=" + fmt.format(date[1]));
        waiting = false;
        countDown--;
    }
}

// Stop timer
timer.cancel();

输出

Waiting
Good: scheduled=57:49.708, actual=57:49.718
Waiting
Good: scheduled=57:49.808, actual=57:49.811
Waiting
Good: scheduled=57:49.908, actual=57:49.920
Good: scheduled=57:49.908, actual=57:49.920
Waiting
Good: scheduled=57:50.008, actual=57:50.014
Waiting
Good: scheduled=57:50.108, actual=57:50.123
Waiting
Good: scheduled=57:50.208, actual=57:50.217
Good: scheduled=57:50.208, actual=57:50.217
Waiting
Good: scheduled=57:50.308, actual=57:50.310
Good: scheduled=57:50.308, actual=57:50.310