【发布时间】:2020-12-30 11:01:11
【问题描述】:
我正在尝试在无限 while 循环中运行 for 循环。当没有 print 语句时,代码不会按预期运行,但是当有 print 语句时,它运行良好。代码如下:
class Test implements Runnable{
public static int varint = 0;
public static void main(String args[]){
Thread x = new Thread(new Test());
int i;
x.start();
while(true){
System.out.println("Hello World"); //If this isn't included,
//the exit statement isn't executed
for(i=0;i<varint;i++){
System.out.println("Exit");
System.exit(0);
}
}
}
public void run(){
try{
Thread.sleep(5000);
} catch(Exception e){
System.out.println("Caught");
}
varint = 1;
}
}
这只是从一个更大的循环中提取的一个小例子。我该如何解决这个问题?
【问题讨论】:
标签: java multithreading loops