0

In discussion at Does a correctly synchronized program still allow data race?(Part I), we got two very good examples.

I just want to discuss the second one. For convenience, I just put second example at here:

public int hashCode() {
    if (hash == 0 && count > 0) { //(1)
        int h = hash;
        int off = offset;
        char val[] = value;
        int len = count;

        for (int i = 0; i < len; i++) {
            h = 31*h + val[off++];
        }
        hash = h; //(2)
    }
    return hash; //(3)
}

According to fist item in definition of happens-before relationship: If x and y are actions of the same thread and x comes before y in program order, then hb(x, y), we may get following conclusion:

there are hb((1),(2)) and hb((2),(3)), hence hb((1),(3)).

Because:

hash is a shared variable;

(1),(2),(3) are all actions of the same thread;

(1) comes before (2), (2) comes before (3) in program order.

Now back to my question: if there is a happens-before relationship between (1) and (3), then (1) and (3) should never be reordered.

Is there any misunderstanding in my explanation? How about your opinion?

4

1 回答 1

2

There is no ordering imposed on actions springing from (1), (2) and (3) commited by different threads; the only happens-before ordering will be within the triplets of (1), (2), (3) commited by the same thread, as per JLS 17.4.5:

If x and y are actions of the same thread and x comes before y in program order, then hb(x, y).

The reordering of machine instructions may happen for any execution, as long as the thread doing them doesn't observe ordering inconsistent with program order. As for any other threads, they can observe the actions in any order (or even not observe them at all) as there is no happens-before going from these actions to any actions of other threads. However, your example contains only a single write and there would have to be at least two writes to give meaning to the concept of "out of order". Only write actions have effects observable from other threads.

于 2012-08-26T12:30:53.313 回答