[英]For and While Loop
I have a program that runs simultaneously and I have this problem where I want to stop the thread but the for loop/while loop doesn't get cancelled once I once I click enter 我有一个同时运行的程序,我有这个问题,我想停止线程,但一旦我点击输入,for循环/ while循环不会被取消
If I take the for
loop out of the while
loop, the program actually responds to the enter and shuts down. 如果我参加了
for
循环出的while
循环,程序实际响应输入并关闭。
class MyNumber extends Thread {
private volatile boolean processing = true;
public void run() {
while (processing) {
// Once I take this for loop out(put // beside it like right now), the enter key to stop the program then does work.
//for(int i = 1; i<27; i++){
System.out.println("Letter " + "i");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// }
}
}
public void permSleep() {
processing = false;
}
}
public class LetterNumber {
public static void main(String[] args) {
MyNumber num1 = new MyNumber();
num1.start();
System.out.println("Hit enter to stop the Numbers!");
Scanner shutter1 = new Scanner(System.in);
shutter1.nextLine();
num1.permSleep();
}
}
Why does the for
loop cause the program to not shutdown? 为什么
for
循环导致程序不关闭?
I'm not really clear on what you're asking. 我不清楚你在问什么。 However, if you're expecting that the
while
and for
loops will both terminate as soon as processing
is set to true
, that isn't what happens. 但是,如果您希望
while
和for
循环在processing
设置为true
立即终止,那就不会发生这种情况。 A while
will execute the statement in the body (ie the for
loop), then it will test the condition ( processing == true
), then if it's true, it executes the statement again, and then tests the condition again. while
将执行正文中的语句(即for
循环), 然后它将测试条件( processing == true
),如果它是真的,它再次执行语句, 然后再次测试条件。 It doesn't test it while the for
loop is executing. 它在
for
循环执行时不会测试它。 It doesn't "notice" when the processing
variable is set. 设置
processing
变量时,它不会“注意到”。 So when processing
is set to true
, the for
loop will keep going until it's done, which could be another 26 seconds. 因此,当
processing
设置为true
, for
循环将继续运行直到完成,这可能是另外26秒。
To fix this simply, add 要解决这个问题,请添加
if (!processing)
break;
inside the for
loop. 在
for
循环中。 Now the processing
flag will be tested each time through the for
loop. 现在,每次通过
for
循环都会测试processing
标志。 (If it were me, I'd put a "label" on the while
loop and use that to break
out of both loops.) Another way to fix it: (如果是我,我会在
while
循环中放置一个“标签”并使用它来break
两个循环。)另一种解决方法:
for(int i = 1; i<27 && processing; i++){
which means the for
loop will continue only as long as processing
is true
. 这意味着只要
processing
为true
, for
循环就会继续。
Note: These solutions will still not test
processing
while the sleep(1000)
is going on. 注意:当
sleep(1000)
正在进行时,这些解决方案仍然不会测试processing
。 So the program could still pause up to 1 second before it terminates. 因此程序在终止之前仍然可以暂停1秒钟。 If you really want a solution that will terminate the
sleep
, you'll have use interrupt
or some other concurrency feature. 如果你真的想要一个终止
sleep
的解决方案,你将使用interrupt
或其他一些并发功能。
It should work. 它应该工作。 Your
for
loop takes about 27
seconds to finish. 你的
for
循环大约需要27
秒才能完成。 It should come out of that after the for
loop has finished. 在
for
循环结束后,它应该出来了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.