【发布时间】:2017-06-11 06:15:33
【问题描述】:
我刚开始在 Java 中使用线程,但在线程内使用 for 循环时遇到问题。
当我在线程中使用 for 循环时,由于某种原因,我看不到要发送到屏幕的输出。
当我使用 while 循环时,它就像一个魅力。
非工作代码如下:
public class ActionsToPerformInThread implements Runnable {
private String string;
public ActionsToPerformInThread(String string){
this.string = string;
}
public void run() {
for (int i = 1; i == 10 ; i++) {
System.out.println(i);
}
}
}
调用代码:
public class Main {
public static void main(String[] args) {
Thread thread1 = new Thread(new ActionsToPerformInThread("Hello"));
Thread thread2 = new Thread(new ActionsToPerformInThread("World"));
thread1.start();
thread2.start();
}
}
我的问题是:为什么当我用 while 循环替换 for 循环并尝试将相同的输出打印到屏幕上时它不起作用?
我尝试调试它,但似乎程序在到达打印的部分之前停止了(没有异常或错误)。
【问题讨论】:
标签: java multithreading for-loop while-loop