0

I got a concurrentModificatoin Exception while working over a HashSet and hence shiftedover to ConcurrentSkipListSet. Although the concurrency issue is solved, i now have a new problem. It seems that a ConcurrentSkipListSet is static by default.Is it so? I'm using it in a recursive function which has an iterator that iterates over the same. When the next element is iterated, the changes made due to another instance of recurrence of the same function is reflected in the same object ( which is not usually the case in normal recursion wherein each instance is allocated its own space in a stack ). And i don't want the changes to be reflected. Is there any solution to this problem?

Thanks in advance :)

I'm using java and here's the code...

public class TSA {

    int[][] l = { { 0, 1, 30, 65535 }, { 50, 0, 15, 5 }, { 65535, 65535, 0, 15 }, { 15, 65535, 5, 0 } };
    int[][] g;


    TSA() {
        int n = 4;
        this.g = this.l;
        int k = 0;
        ConcurrentSkipListSet a = new ConcurrentSkipListSet();

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                this.g[i][j] = this.l[i][j];
            }
        }

        System.out.println(this.g[2][1]);
        a.add(2);
        a.add(3);
        a.add(4);

        Iterator ir = a.iterator();
        int[] path = new int[n];
        k = 0;

        while (ir.hasNext()) {
            ConcurrentSkipListSet b = new ConcurrentSkipListSet();
            b = a;
            b.add(2);
            b.add(3);
            b.add(4);

            int next = (Integer) ir.next();
            System.out.println("next in main is   " + next);
            System.out.println("called with   " + b);

            path[k++] = this.l[0][next - 1] + gfun(next, b);
            System.out.println("min path is..." + path[k - 1]);

        }
    }


    public static void main(final String[] args) {
        new TSA();
    }


    int gfun(final int next, final ConcurrentSkipListSet d) {
        ConcurrentSkipListSet b = d;

        if (b.size() != 1 && b.size() != 2) {
            b.remove(next);
            System.out.println(b);

            int[] path = new int[b.size()];
            int k = 0;

            Iterator ir = b.iterator();

            while (ir.hasNext()) {
                int a = (Integer) ir.next();
                System.out.println(a + "     iterator prob   " + b);
                path[k] = this.l[next - 1][a - 1] + gfun(a, b);
                System.out.println("path[" + k + "] is" + path[k]);
                k = k + 1;

            }

            return min(path);
        }
        else if (b.size() == 2) {
            System.out.println("second instance..." + next + ".." + b);
            Iterator irrr = b.iterator();
            int a = (Integer) irrr.next();
            b.remove(next);
            return (gfun(next, b));
        }
        else {
            Iterator irr = b.iterator();

            int j = (Integer) irr.next();
            System.out.println("inside prog size is 1" + b);
            System.out.println("l[" + next + "][" + j + "] is  " + this.l[next - 1][j - 1]);
            int ans = this.l[next - 1][j - 1] + this.g[j - 1][0];
            System.out.println("l[" + next + "][" + j + "]+g[" + j + "][1] which is   " + ans + "  is                                   r               returned");

            return (ans);
        }

    }


    private int min(final int[] path) {
        int m = path[0];

        for (int i = 0; i < path.length; i++) {
            if (path[i] < m) {
                m = path[i];
            }
        }
        return m;
    }
}

I'm removing the elements from set b and when the function returns to a previous state( iterator inside the while loop) the original set b is affected. I don't want this to happen. Somebody please help! :(

Fabian Barney
  • 14,219
  • 5
  • 40
  • 60

1 Answers1

0

You've some basic misunderstanding how references and variables work in Java. In general you only create a new object when you see the new keyword like with new ConcurrentSkipListSet(). (There are some special cases how to create objects without seeing some new keyword but imho you can omit this in the first place.)

All assignments from one variable to another variable do NOT create new objects. You assign the variable to the same object as the other. These are references to the same object.

You have this code for example:

    while (ir.hasNext()) {
        ConcurrentSkipListSet b = new ConcurrentSkipListSet();
        b = a; // 'b' now refers to the same object as 'a'.
        ...
    }

This is complete non-sense. You create a new list and drop it in the next line, because now you point the reference b to the same object the reference a is pointing at.

That is why you effectively only use the list object you created first in all your code:

    ConcurrentSkipListSet a = new ConcurrentSkipListSet();

Very much the same with your two-dimensional array. You only created one, assigned it to l and pointed g later in the code to the same array l is pointing to. After that you copy some values but in fact you copy values from and to the SAME array object. This is complete non-sense again. Push the line System.out.println(this.g[2][1]); in top of your for loops and I bet you will be surprised.

Have a look at this SO answer which @RC. posted in his comment. It is addressing the source of your misunderstanding.

Community
  • 1
  • 1
Fabian Barney
  • 14,219
  • 5
  • 40
  • 60