java LongAdder: How can the try block fail?
up vote
3
down vote
favorite
I'm analyzing the java LongAdder algorithm in detail. LongAdder extends class Striped64 and there the essential method is retryUpdate. The following peace of code is taken from this method; in the linked source code it occupies lines 212 - 222:
try { // Recheck under lock
Cell rs; int m, j;
if ( (rs = cells) != null &&
(m = rs.length) > 0 &&
rs[j = (m - 1) & h] == null) {
rs[j] = r;
created = true;
}
} finally {
busy = 0;
}
Question: How can this try block fail?
N.b.: The access
rs[j = (m - 1) & h]
shouldn't throw an IndexOutOfBoundsException exception because the result of a bitwise and-operation is always less or equal than the minimum of its integer arguments, hence 0 <= j <= m-1 is within the bounds of the array.
java algorithm
add a comment |
up vote
3
down vote
favorite
I'm analyzing the java LongAdder algorithm in detail. LongAdder extends class Striped64 and there the essential method is retryUpdate. The following peace of code is taken from this method; in the linked source code it occupies lines 212 - 222:
try { // Recheck under lock
Cell rs; int m, j;
if ( (rs = cells) != null &&
(m = rs.length) > 0 &&
rs[j = (m - 1) & h] == null) {
rs[j] = r;
created = true;
}
} finally {
busy = 0;
}
Question: How can this try block fail?
N.b.: The access
rs[j = (m - 1) & h]
shouldn't throw an IndexOutOfBoundsException exception because the result of a bitwise and-operation is always less or equal than the minimum of its integer arguments, hence 0 <= j <= m-1 is within the bounds of the array.
java algorithm
2
This appears to be defensive coding. It could fail if the code changes in ways the original developer didn't expect.
– Peter Lawrey
Nov 7 at 19:56
1
What Peter said but the form also hints at the mechanics: a "sort of" lock is acquired incasBusy()
and the convention is to always release the lock in a finally block (busy = 0
in this case). Following the convention, even though not strictly needed, makes the (structure of the) code easier to read and understand (at least for me).
– vanOekel
Nov 7 at 20:55
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I'm analyzing the java LongAdder algorithm in detail. LongAdder extends class Striped64 and there the essential method is retryUpdate. The following peace of code is taken from this method; in the linked source code it occupies lines 212 - 222:
try { // Recheck under lock
Cell rs; int m, j;
if ( (rs = cells) != null &&
(m = rs.length) > 0 &&
rs[j = (m - 1) & h] == null) {
rs[j] = r;
created = true;
}
} finally {
busy = 0;
}
Question: How can this try block fail?
N.b.: The access
rs[j = (m - 1) & h]
shouldn't throw an IndexOutOfBoundsException exception because the result of a bitwise and-operation is always less or equal than the minimum of its integer arguments, hence 0 <= j <= m-1 is within the bounds of the array.
java algorithm
I'm analyzing the java LongAdder algorithm in detail. LongAdder extends class Striped64 and there the essential method is retryUpdate. The following peace of code is taken from this method; in the linked source code it occupies lines 212 - 222:
try { // Recheck under lock
Cell rs; int m, j;
if ( (rs = cells) != null &&
(m = rs.length) > 0 &&
rs[j = (m - 1) & h] == null) {
rs[j] = r;
created = true;
}
} finally {
busy = 0;
}
Question: How can this try block fail?
N.b.: The access
rs[j = (m - 1) & h]
shouldn't throw an IndexOutOfBoundsException exception because the result of a bitwise and-operation is always less or equal than the minimum of its integer arguments, hence 0 <= j <= m-1 is within the bounds of the array.
java algorithm
java algorithm
edited Nov 7 at 19:22
asked Nov 7 at 18:39
user120513
12910
12910
2
This appears to be defensive coding. It could fail if the code changes in ways the original developer didn't expect.
– Peter Lawrey
Nov 7 at 19:56
1
What Peter said but the form also hints at the mechanics: a "sort of" lock is acquired incasBusy()
and the convention is to always release the lock in a finally block (busy = 0
in this case). Following the convention, even though not strictly needed, makes the (structure of the) code easier to read and understand (at least for me).
– vanOekel
Nov 7 at 20:55
add a comment |
2
This appears to be defensive coding. It could fail if the code changes in ways the original developer didn't expect.
– Peter Lawrey
Nov 7 at 19:56
1
What Peter said but the form also hints at the mechanics: a "sort of" lock is acquired incasBusy()
and the convention is to always release the lock in a finally block (busy = 0
in this case). Following the convention, even though not strictly needed, makes the (structure of the) code easier to read and understand (at least for me).
– vanOekel
Nov 7 at 20:55
2
2
This appears to be defensive coding. It could fail if the code changes in ways the original developer didn't expect.
– Peter Lawrey
Nov 7 at 19:56
This appears to be defensive coding. It could fail if the code changes in ways the original developer didn't expect.
– Peter Lawrey
Nov 7 at 19:56
1
1
What Peter said but the form also hints at the mechanics: a "sort of" lock is acquired in
casBusy()
and the convention is to always release the lock in a finally block (busy = 0
in this case). Following the convention, even though not strictly needed, makes the (structure of the) code easier to read and understand (at least for me).– vanOekel
Nov 7 at 20:55
What Peter said but the form also hints at the mechanics: a "sort of" lock is acquired in
casBusy()
and the convention is to always release the lock in a finally block (busy = 0
in this case). Following the convention, even though not strictly needed, makes the (structure of the) code easier to read and understand (at least for me).– vanOekel
Nov 7 at 20:55
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195759%2fjava-longadder-how-can-the-try-block-fail%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
This appears to be defensive coding. It could fail if the code changes in ways the original developer didn't expect.
– Peter Lawrey
Nov 7 at 19:56
1
What Peter said but the form also hints at the mechanics: a "sort of" lock is acquired in
casBusy()
and the convention is to always release the lock in a finally block (busy = 0
in this case). Following the convention, even though not strictly needed, makes the (structure of the) code easier to read and understand (at least for me).– vanOekel
Nov 7 at 20:55