-3

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?

Community
  • 1
  • 1
baby boom
  • 3
  • 1
  • 6
  • Firstly the `y` array does not make much sense to me, can't it be just an `int`? Secondly, the code is not thread safe, so its behavior is undefined, I mean the counts of 0's and 1's are random. All those instance variables used in the `run` must be synchronized in some way. – neevek May 19 '12 at 17:54
  • thats because we want to create race condition. that way both threads work on the same array. I'm interested in the limits of 1's – baby boom May 19 '12 at 17:56
  • Oh, I just read the code in `main` method, and your code has no race condition, you just created 2 separated threads, which don't share the same `Runnable` object. And let's hypothesize your code has race condition, there will be at least **10** 1's and at most **20** 1's. – neevek May 19 '12 at 18:14
  • 1
    You posted this question 8 hours ago: http://stackoverflow.com/questions/10664346/java-threads-effect-on-static-classes – Tudor May 19 '12 at 18:35
  • 1
    Exact duplicate. Why ask the same question twice? Voted to close. – Jivings May 19 '12 at 18:49
  • NOT THE SAME 2 lines missing! it's a completely different situation – baby boom May 19 '12 at 19:12
  • @babyboom Seems pretty similar to me. – Jivings May 20 '12 at 00:27
  • After you edited the your code, it is a exact duplicate. – neevek May 20 '12 at 01:13

3 Answers3

0

The max number is 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 +2 + 1; lets say that one thread performed 10 times write 1. than the other thread with value y[0] == 0 perform 1 time. the first thread can again run the loop up to 10 from the place where the second thread stop.

Horonchik
  • 477
  • 2
  • 11
0

First, see my comments on the question.

    public void run() {
        x = 0;

        y[0] = 0;             // line a
        while (x< 10) {      
            ++x;
            System.out.print("0");
        }

        while (y[0] < 10) {   // line b
            ++y[0];
            System.out.print('1');
        }
    }

If thread A and thread B reach line b and line a respectively at the same moment, there will be 10 1's printed. If thread A is running its loop and y[0] is just incremented to 10 but is not yet tested for truth/falsehood, and at this moment, thread B reaches line a, and it changes y[0] to 0, then thread A gains 10 more chances to print "1", so the count is 20.

neevek
  • 11,760
  • 8
  • 55
  • 73
0

The min amount of 1s is obviously 10, the max amount will be 20.

20 because worst case will be that both threads reach

while (y[0] < 10) 

at the same time every time, and then again reach

++y[0];

also at the same time every time, which will render one of the increments lost.

stryba
  • 1,979
  • 13
  • 19