Possible Duplicate:
java threads effect on static classes
consider the following code:
static class ThreadTest extends Thread {
int x;
int[] y;
public ThreadTest(int x, int[] y) {
this.x = x;
this.y = y;
}
@Override
public void run() {
while (x< 10) {
++x;
System.out.print("0");
}
while (y[0] < 10) {
++y[0];
System.out.print('1');
}
}
}
public static void main(String args[]) {
int x = 0;
int[] y = new int[1];
y[0] = 0;
Thread A = new ThreadTest(x, y);
Thread B = new ThreadTest(x, y);
B.start();
A.start();
}
there is clearly a race condition. how can i evaluate the maximum and minimum number of 1's that will be printed? what may cause change in the number of 1 printed?