6
I am having some problems in the complete understanding of the code because I am new to using java in thread. What code will do is control the flow, doing 55 iterations dividing between the thread and main program (main):
Resultado:
Main Thread.: 1
New Thread..: 2
Main Thread.: 3
New Thread..: 4
...
Main Thread.: 51
New Thread..: 52
Main Thread.: 53
New Thread..: 54
So far so good , the problem is , when in a certain execution the program enters in some infinite loop and does not stop to run again it completes the execution , wanted help to understand this problem.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
/
package testando;
/*
*
* @author Márcio
*/
public class Testando {
static int value = 0;
static int flag = 1;
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
class MyThread extends Thread {
@Override
public void run() {
while(flag == 1);
while(++value < 55) {
System.out.println("New Thread..: " + value);
flag = 1;
while( value < 55 && flag == 1);
}
}
}
Thread thread1 = new MyThread();
thread1.start();
System.out.println(flag == 0);
while(flag == 0);
while(++value < 55) {
System.out.println("Main Thread.: " + value);
flag = 0;
while( value < 55 && flag == 0);
}
}
}
while(flag == 1);
why this?– Henrique Barcelos
My was testing here, see the code run without this function, and also without
while(flag == 0);
, I ask you to disregard this part.– Marcio Albuquerque
Could remove unnecessary tags within your question and code?
– Leonardo Otto