java LongAdder: How can the try block fail?











up vote
3
down vote

favorite
1












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.










share|improve this question




















  • 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

















up vote
3
down vote

favorite
1












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.










share|improve this question




















  • 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















up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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










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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini