I'm having problems understanding the code completely because I'm new to using java in thread. What code will do is to control the flow by doing 55 iterations by dividing the thread and main program:
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 certain execution the program goes into some infinite loop and does not stop executing another time it completes the execution, I 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);
}
}
}