I just started to use threads in Java and I'm having issues with using for loop inside a thread.
When I am using a for loop inside the thread, from some reason I cannot see the outputs that I am sending to the screen.
When I am using the while loop it works like a charm.
The non-working code is the following:
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);
}
}
}
Calling code:
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();
}
}
My question is: why when I'm replacing the for-loop with while-loop and try to print the same output into the screen it does not work?
I tried to debug it but it seems like the program stopped before getting to the part that its printing (There is not exception or error).